Home > Web Front-end > JS Tutorial > JS function calls and issues related to implicit parameters arguments and this

JS function calls and issues related to implicit parameters arguments and this

一个新手
Release: 2017-09-28 09:21:40
Original
1421 people have browsed it

##1. Several different ways of calling functions:

#1. Calling as a function

#2. Calling as a method, Call on the object, support object-oriented programming

#3. Call as a constructor to create a new object#4. Call through the apply() or call() method, This method is relatively the most complicated


##2. Function calls will pass two implicit parameters and exist in the scope of the function: arguments and this

Function parameters How to deal with inconsistencies with formal parameters:
#1. Actual parameters>Formal parameters--》The excess actual parameters will not be allocated

#2. Actual parameters

##3.

#1. Function context: depends on the calling method of the function (four calling methods).

#2. arguments: a collection of all parameters, with a length attribute, which can be obtained using arguments[2], etc.; but arguments are not arrays, and those methods without arrays


#3. this: References an object that is implicitly associated with the function call, called the function context

##Four:

#1. "As a function" call: Refers to the first method of the above four methods, which is different from the other three methods
For example: function test(a,b){}/var test = function(a,b){}
The function context at this time It is window, that is, **this==window**


#2. "As a method" call: When a function is assigned to an object and the attribute that references the function is used
I. The object to which a method belongs can be called with this in the method body

var o = {};
o.test = function(){};
o.test();
Copy after login

The function context at this time is the object o, that is, **this==o**




#3. "As constructor" call: Same as function declaration, except how to call the function, use the new keyword before the function call
function test(){return this};

new test();#########Special features of the constructor:#######I. Create a new empty object#######II. Pass to the constructor The object is this object, thus becoming the context of the constructor#######III. If there is no explicit return value, the newly created object is returned as the return value of the constructor######
function Ninja(){
this.skulk = function(){return this};
//无显式返回,故返回this===o
}===function Ninja(){
var o = {};
o.skulk = function(){return this;};
return o;
}
var ninja1 = new Ninja();
var ninja2 = new Ninja();
ninja1.skulk === ninja1;
ninja2.skulk === ninja2;
Copy after login
# ######apply() and call() methods: Explicitly specify any object as a function context ###apply() requires two parameters: 1. The object as the function context 2. Composed of function parameters The array###call() also requires two parameters: the difference is that the second parameter is a parameter list########You can allow any element to be used as the context of apply() and call()###

The above is the detailed content of JS function calls and issues related to implicit parameters arguments and this. 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