Home >Web Front-end >Front-end Q&A >What is the difference between javascript objects and functions
Difference: 1. JavaScript objects are containers of variables, which can encapsulate functions, and functions are blocks of code designed to perform specific tasks; 2. JavaScript objects cannot encapsulate the operation process, but JavaScript functions can encapsulate them. Operation process.
The operating environment of this tutorial: Windows 10 system, JavaScript version 1.8.5, Dell G3 computer.
1. Object
"JavaScript" Object is a container of variables, but usually we think of an object as a container of key-value pairs. The usual way to write a key-value pair is name:value (the key and value are separated by a colon).
Key-value pairs are usually called object properties in javaScript objects.
Example:
var person = { firstName:"zhang", lastName:"erga", fullName: function() { return this.firstName + " " + this.lastName; } }
2. Function
Function (method Function) is an encapsulation of the js operation process. To operate the same process in the future, just call the corresponding function (method ). Objects also encapsulate js code, but objects can encapsulate functions (methods). For example, encapsulate functions (methods) of a certain type into an object. This allows the system to manage calling functions (methods).
Example:
function sum(num1, num2) { return num1 + num2; }
3. Contact
Functions can be encapsulated in the object.
Example:
var person = { firstName:"zhang", lastName:"erga", fullName: function() { return this.firstName + " " + this.lastName; } }
4. Difference
The object contains and can only be key-value pairs (key values are separated by ":", and the value content includes ordinary variables and functions).
The operation process can be encapsulated in the function, but the operation process cannot be encapsulated in the object.
Related recommendations: javascript learning tutorial
The above is the detailed content of What is the difference between javascript objects and functions. For more information, please follow other related articles on the PHP Chinese website!