JavaScript function calls

The function body code will be executed when the function is called.

Javascript functions have 4 calling methods:

Ordinary function call

Method call

Constructor call

By call( ) and apply() indirect calls


##1. Ordinary function calls

The most commonly used function calling methods, such as:

alert("hello");

##var result = add(1, 2);


2. Method call

//Define a function

function hello(name) {

alert('hello,' + name) ;

};

var user = {};

// Assigned to user’s sayHi attribute
##user.sayHi = hello;

//Method call


user.sayHi('Zhang San');

One difference between ordinary function calls and method calls: in "ordinary In the "function call" method, the calling context of the function (the value of this) is the global object (non-strict mode) or undefined (strict mode). In the "method call" method, this points to the current object. Using this feature, we can directly return this when the method does not require a clear return value, thereby realizing "method chaining".

    php中文网(php.cn) 

This in nested functions: In nested functions, the inner function will not inherit the this of the outer function, that is, when the inner function is called as a method, the this of the inner function points to the current calling object. ;When the inner function is called as a function, the value of this is the global object (non-strict mode) or undefined (strict mode). How to access this of the outer function in the inner function? This is usually saved in a local variable and accessed through variables:

var obj = {

f : function() {

var self = this;

console.log(this === obj);//true, this points to the current object

F1 ();

This

# console.log(this === obj);//false, this is a global object or undefined

## console.log(self === obj);// true, self points to the outer this, that is, the current object

}

}

} ;

3. Constructor call

When you use the new keyword to create an object, the constructor is called. If the constructor has no formal parameters, you can omit the parentheses:

var obj = new Object();

//Equivalent to

var obj = new Object;

Call the constructor and create a new object. This new object will become the calling context of the constructor (the value of this):

function User(name) {

this.name=name;

console.debug(this);

##}

var user = new User('Zhang San');

    php中文网(php.cn) 

4. Through call() and apply( ) Indirect call

Functions in Javascript are also objects and have methods. Among them, call() and apply() can be used to call functions indirectly. The first parameter of call() is used to specify the calling context (that is, the value of this), and the subsequent parameters are the actual parameters passed into the calling function.

var name = 'A';

var user = {

name : 'B'

};

function showName() {

# # alert(this.name);

}

##showName();//A, this is a global object


showName.call(user);//B, this is the user object

apply() is similar to call(), but the difference The problem is that the subsequent actual parameters need to be passed in the form of an array (the arguments array of the current function can be passed in directly).


Continuing Learning
||
php中文网(php.cn)
submit Reset Code
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!