Get the App
SLTechnology News&Howtos  ›  Development  › 

What are the differences between javascript objects and functions

Shulou Source: shulou.com Published: 2022-06-01 10:28:16 10月28日 Update

This article focuses on "what are the differences between javascript objects and functions". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor learn "what's the difference between javascript objects and functions"?

Differences: 1, the JavaScript object is a container of variables, which can encapsulate functions, while functions are designed to perform specific tasks of code blocks; 2, JavaScript objects can not encapsulate the operation process, while the JavaScript function can encapsulate the operation process.

The operating environment of this tutorial: windows10 system, javascript1.8.5 version, Dell G3 computer.

What is the difference between a javascript object and a function

1. Object

The "JavaScript" object is a container for variables, but usually we think of it as a container for key-value pairs, which are usually written as name:value (keys and values are separated by colons).

Key-value pairs are often called object properties in javaScript objects.

Example:

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

two。 Function

The function (method Function) is the encapsulation of the js operation process. To operate the same process later, you only need to call the corresponding function (method). Objects are also encapsulated in js code, but objects can encapsulate functions (methods). For example, encapsulate a certain class of functions (methods) into an object. This allows the management of the system to call functions (methods).

Example:

Function sum (num1, num2) {return num1 + num2;}

3. contact

Functions can be encapsulated in an 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 (the key-value is divided by ":", and the value content includes ordinary variables and functions).

The operation process can be encapsulated in the function, but not in the object.

At this point, I believe you have a deeper understanding of "what is the difference between javascript objects and functions?" you might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: Objects functions encapsulation methods procedures examples content variables containers code systems learning practical common deeper tasks interests content including colons writing Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info NVidia Redmi Docker MariaDB