Home > Web Front-end > JS Tutorial > body text

Summary of examples of function calling patterns in javascript basic tutorials

伊谢尔伦
Release: 2017-07-25 10:27:25
Original
1181 people have browsed it

It will only be executed when the function is called. The call operator is a pair of parentheses following any expression that produces a function value. The parentheses can contain zero or more comma-separated expressions. Each expression produces a parameter value, and each parameter value is assigned the formal parameter name

defined when the function was declared. JavaScript has a total of 4 calling modes: function calling mode, method calling mode, constructor calling mode and Indirect calling mode

【1】Function calling mode

When a function is not a property of an object, it is called as a function. For ordinary function calls, the return value of the function is the value of the calling expression

function add(x,y){    
   return x+y;
}
var sum = add(3,4);
console.log(sum)//7
Copy after login

When calling a function using the function call mode, in non-strict mode, this is bound to the global object; in strict mode , this is undefined

function add(x,y){
    console.log(this);//window}    
add();
Copy after login
function add(x,y){    
'use strict';
    console.log(this);//undefined
}    
add();//window
Copy after login

Therefore, 'this' can be used to determine whether the current is strict mode

var strict = (function(){return !this;}());
Copy after login

Rewrite

Because this is bound to the function in the function call mode Determined to the global object, so the phenomenon of global properties being overwritten will occur

var a = 0;
function fn(){
    this.a = 1;
}
fn();
console.log(this,this.a,a);//window 1 1
Copy after login

[2] Method calling mode

When a function is saved as an attribute of the object, we call it a method. When a method is called, this is bound to the object. If the calling expression contains an action to extract properties, then it is called as a method.

var o = {
    m: function(){
        console.log(1);
    }
};
o.m();//1
Copy after login

The method can use this to access the object it belongs to, so it can get the value from the object or modify the object. . The binding of this to the object occurs at call time. Methods that can obtain the context of the objects they belong to through this are called public methods

var o = {
    a: 1,
    m: function(){
        return this;
    },
    n: function(){
        this.a = 2;
    }
};
console.log(o.m().a);//1
o.n();
console.log(o.m().a);//2
Copy after login

Any function that is called as a method will actually pass in an implicit argument - this argument is an object, and the method call The parent is this object. Generally speaking, methods based on that object can perform a variety of operations. The syntax of the method call has clearly shown that the function will operate based on an object.

rect.setSize(width,height);
setRectSize(rect,width,height);
Copy after login

Assume the above two lines of code The functions are exactly the same, they all act on a hypothetical object rect. It can be seen that the method calling syntax in the first line clearly indicates that the carrier of this function execution is the rect object, and all operations in the function will be based on this object

Unlike variables, the keyword this has no scope Due to the restriction, a nested function does not inherit this from the function that calls it. If a nested function is called as a method, its this value points to the object that called it. If the nested function is called as a function, its this value is either a global object (in non-strict mode) or undefined (in strict mode)

var o = {
    m: function(){
         function n(){
             return this;
         }
         return n();
    }
}
console.log(o.m());//window
Copy after login
var o = {
    m: function(){
         function n(){
             'use strict';
             return this;
         }
         return n();
    }
}
console.log(o.m());//undefined
Copy after login

If you want to access the this value of this external function, you need to save the value of this In a variable, both the variable and the internal function are in the same scope. Usually the variable self or that is used to save this

var o = {
    m: function(){
        var self = this;
        console.log(this === o);//true
         function n(){
             console.log(this === o);//false
             console.log(self === o);//true
             return self;
         }
         return n();
    }
}
console.log(o.m() === o);//true
Copy after login

【3】Constructor call mode

If the function or method call is preceded by the keyword new, it constitutes a constructor call

function fn(){    
   this.a = 1;
};
var obj = new fn();
console.log(obj.a);//1
Copy after login

 If the constructor call contains a set of actual parameter lists in parentheses, these actual parameter expressions are first calculated and then passed into the function

function fn(x){    
  this.a = x;
};
var obj = new fn(2);
console.log(obj.a);//2
Copy after login

 If the constructor has no formal parameters, the javascript constructor call The syntax of allows the omission of the argument list and parentheses. Any constructor call without formal parameters can omit the parentheses

var o = new Object();//等价于
var o = new Object;
Copy after login

[Note] Although the constructor looks like a method call, it will still use the new object as the calling context. That is to say, in the expression new o.m(), the calling context is not o

var o = {
    m: function(){
        return this;
    }
}
var obj = new o.m();
console.log(obj,obj === o);//{} false
console.log(obj.constructor === o.m);//true
Copy after login

Constructors usually do not use the return keyword. They usually initialize new objects. When the function body of the constructor is executed, It is returned explicitly. In this case, the constructor call expression evaluates to the value of this new object

function fn(){    
  this.a = 2;
}var test = new fn();
console.log(test);//{a:2}
Copy after login

If the constructor uses a return statement but does not specify a return value, or returns a primitive value, then this will be ignored Return the value and use this new object as the call result

function fn(){
    this.a = 2;
    return;
}
var test = new fn();
console.log(test);//{a:2}
Copy after login

If the constructor explicitly uses the return statement to return an object, then the value of the calling expression is this object

var obj = {a:1};
function fn(){
    this.a = 2;
    return obj;
}
var test = new fn();
console.log(test);//{a:1}
Copy after login

[4] Indirect calling mode

Functions in JavaScript are also objects, and function objects can also contain methods. The call() and apply() methods can be used to call functions indirectly

Both of these methods allow you to explicitly specify the this value required for the call, that is, any function can be used as a method of any object. Called, even if this function is not a method of that object. Both methods can specify the actual parameters of the call. The call() method uses its own actual parameter list as the actual parameter of the function, and the apply() method requires the parameters to be passed in in the form of an array

var obj = {};
function sum(x,y){
    return x+y;
}
console.log(sum.call(obj,1,2));//3
console.log(sum.apply(obj,[1,2]));//3
Copy after login

The above is the detailed content of Summary of examples of function calling patterns in javascript basic tutorials. 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!