Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand the concepts of function, built-in object and this of js

2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "how to understand js's function, built-in object, this concept". In daily operation, I believe many people have doubts about how to understand js's function, built-in object and this concept. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the doubt of "how to understand js's function, built-in object, this concept". Next, please follow the editor to study!

Functions in javascript are different from other languages in that each function is maintained and run as an object. Through the properties of the function object, it is convenient to assign a function to a variable or pass the function as a parameter. Before moving on, let's take a look at the syntax for the use of functions:

The following is a reference clip:

Function func1 (…) {... }

Var func2=function (…) {... }

Var func3=function func4 (…) {... }

Var func5=new Function ()

These are the correct syntax for declaring functions. They are very different from the functions that are common in other languages or the way functions are defined previously. So why can it be written like this in JavaScript? What grammar does it follow? These are described below.

Recognize function objects (Function Object)

You can define a function with the function keyword and specify a function name for each function, which is called by the function name. When the JavaScript interpretation is executed, the function is maintained as an object, which is the function object (Function Object) to be introduced.

Function objects are essentially different from other user-defined objects. These objects are called internal objects, such as date objects (Date), array objects (Array), and string objects.

(String) are internal objects. The constructors for these built-in objects are defined by JavaScript itself: by executing new

Statements like Array () return an object, and there is a mechanism within JavaScript to initialize the returned object, rather than for the user to specify how the object is constructed.

In JavaScript, the corresponding type of function object is Function, just as the corresponding type of array object is Array and the corresponding type of date object is Date.

Pass new

Function () to create a function object, or you can use the function keyword to create an object. For ease of understanding, we compare the creation of function objects and the creation of array objects. First

Look at the array object: the following two lines of code create an array object myArray:

The following is a reference clip:

Var myArray= []

/ / equivalent to

Var myArray=new Array ()

Similarly, the following two pieces of code create a function myFunction:

Function myFunction (a _ r _ b) {

Return aquib

}

/ / equivalent to

Var myFunction=new Function ("a", "b", "return aquib")

By comparing with the construction array object statement, you can clearly see the essence of the function object. The function declaration described above is the first way of the above code, and inside the interpreter, when you encounter this syntax, you will

Dynamically construct a Function object and store and run the function as an internal object. You can also see that a function object name (function variable) has the same name as an ordinary variable name.

Specification, can refer to this variable by the variable name, but the function variable name can be followed by parentheses and argument list to make the function call.

It is not common to create a function in the form of new Function (), because a function body usually has multiple statements, and if you pass them as a string as an argument, the code is less readable. The syntax for its use is described below:

The following is a reference clip:

Var funcName=new Function (p1, p2, and pn, respectively)

The types of parameters are strings, p1 to pn represent the list of parameter names of the created function, body represents the function body statement of the created function, and funcName is the name of the created function. You can create an empty function without specifying any arguments and an unnamed function without specifying funcName, but of course that kind of function doesn't make any sense.

It should be noted that p1 to pn is a list of parameter names, that is, p1 can not only represent a parameter, it can also be a comma-separated list of parameters, for example, the following definitions are equivalent:

The following is a reference clip:

New Function ("a", "b", "c", "return a+b+c")

New Function ("a, b, c", "return a+b+c")

New Function ("aforme b", "c", "return a+b+c")

JavaScript introduces the Function type and provides syntax such as new Function () because the Function type must be used to add properties and methods to function objects.

The essence of a function is an internal object, which is determined by the JavaScript interpreter. The function created by the above code can be called using the function name in the program. The function definition issues listed at the beginning of this section are also explained. Note that you can directly add parentheses after the function declaration to indicate that the function call is made immediately after the creation is completed, for example:

The following is a reference clip:

Var i=function (a _ r _ b) {

Return aquib

} (1Pol 2)

Alert (I)

This code shows that the value of the variable I is equal to 3. I represents the returned value, not the created function, because parentheses "(" higher than the equal sign "=" have higher priority. Such code may not be commonly used, but it is a good solution when users want to modularize long code snippets or want to avoid naming conflicts.

It is important to note that although the following two ways to create functions are equivalent:

The following is a reference clip:

Function funcName () {

/ / function body

}

/ / equivalent to

Var funcName=function () {

/ / function body

}

But the former method creates a named function, while the latter creates an unnamed function, just letting a variable point to the unnamed function. There is only one difference in use: for a named function, it can be defined after the call, while for an unnamed function, it must be defined before it is called. For example:

The following is a reference clip:

< script language= "JavaScript" type= "text/javascript" >

Share To

Development

© 2024 shulou.com SLNews company. All rights reserved.

12
Report