Home > Article > Web Front-end > JS function calls and issues related to implicit parameters arguments and this
##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: ##3. ##Four: #1. "As a function" call: Refers to the first method of the above four methods, which is different from the other three methods
#1. Actual parameters>Formal parameters--》The excess actual parameters will not be allocated
#2. Actual parameters
#1. Function context: depends on the calling method of the function (four calling methods).
#3. this: References an object that is implicitly associated with the function call, called the function context
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 bodyvar o = {};
o.test = function(){};
o.test();
#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};
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;# ######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!