Home  >  Article  >  Web Front-end  >  Summary of various calling methods of named functions in JavaScript_javascript skills

Summary of various calling methods of named functions in JavaScript_javascript skills

WBOY
WBOYOriginal
2016-05-16 16:32:071561browse

A previous article mentioned Multiple ways to call anonymous functions. This article looks at the various ways to call named functions.

1, ()

The most commonly used one is the () operator to call/execute a function:

Copy code The code is as follows:

// Parameterless function fun1
function fun1() {
alert('I've been called');
}
fun1();

// Parameterized function fun2
function fun2(param) {
alert(param);
}
fun2('I was called');

After ECMAScript3 added call and apply to Function, there are the following two types.

2. call

Copy code The code is as follows:

// Parameterless function fun1
function fun1() {
alert('I've been called');
}
fun1.call(null);

// Parameterized function fun2
function fun2(param) {
alert(param);
}
fun2.call(null,'I was called')

3. apply

Copy code The code is as follows:

// Parameterless function fun1
function fun1() {
alert('I've been called');
}
fun1.apply(null);

// Parameterized function fun2
function fun2(param) {
alert(param);
}
fun2.apply(null,['I was called'])

Although call and apply can be purely used to call/execute functions, they are more used to change the context of function execution.

4. new (this method is not recommended)

Copy code The code is as follows:

// Parameterless function fun1
function fun1() {
alert('I've been called');
}
new fun1();

// Parameterized function fun2
function fun2(param) {
alert(param);
}
new fun2('I was called')

The essence of new is to create/construct an instance of a class. The fun1 and fun2 defined here are obviously not a class (no this, no prototype). But both functions did execute. This is a side effect of new.

From the above calling methods, there is no difference in the execution results of the four methods. But if the function has a return value, you may be a little disappointed when calling it with new.

Copy code The code is as follows:

// Function fun
with return value function fun() {
alert('I've been called');
Return "jack";
}
var c = new fun();
alert(c);//[object Object], why not "jack"?

Change it to this

Copy code The code is as follows:

// Function fun
with return value function fun() {
alert('I've been called');
Return {name:'jack'};
}
var c = new fun();
alert(c.name); //jack, returned normally

To summarize: when calling a function using new method. If there is a return value, when the return value is a built-in type (basic type) of JavaScript such as String, Number, Boolean, etc., the value will not be returned; when the return value is an object, the function , array and other object types, the object, function, array will be returned directly.

When the return value is a built-in type (basic type), what exactly does new fun() return? The next article will discuss the details of the new method call.

Statement:
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