Home > Web Front-end > Front-end Q&A > What is the difference between javascript objects and functions

What is the difference between javascript objects and functions

WBOY
Release: 2022-03-03 12:33:29
Original
3005 people have browsed it

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.

What is the difference between javascript objects and functions

The operating environment of this tutorial: Windows 10 system, JavaScript version 1.8.5, Dell G3 computer.

What is the difference between javascript objects and functions

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;
    }
}
Copy after login
Copy after login

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; 
}
Copy after login

3. Contact

Functions can be encapsulated in the object.

Example:

var person = {
    firstName:"zhang",
    lastName:"erga",
    fullName: function() {
        return this.firstName + " " + this.lastName;
    }
}
Copy after login
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template