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

Four ways to call named functions in javascript Recommended page 1/3_javascript skills

PHP中文网
Release: 2016-05-16 18:50:38
Original
1101 people have browsed it

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.

1. () parentheses operator The most commonly used one is the () operator to call a function

Copy code The code is as follows:


//No parameter function fun1
function fun1() {
alert('I was called');
}
fun1()

/ / Function fun2 with parameters
function fun2(param) {
alert(param);
}
fun2('I was called')


After adding call and apply to Function after ECMAScript 3, there are two types of
2. call

Copy code The code is as follows:


//No parameter function fun1
function fun1() {
alert('I was called');
}
fun1.call(null);

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


3. apply

Copy code The code is as follows:


//No parameter function fun1
function fun1() {
alert('I was called');
}
fun1.apply(null);

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


4. new (not recommended Use this method )

Copy the code The code is as follows:


//No parameter function fun1
function fun1( ) {
alert('I was called');
}
new fun1();


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


ok, from the above calling method, there are four execution methods There is no difference in the results. 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 a return value
function fun() {
alert( 'I was called');
return "jack";
}

var c = new fun();
alert(c);//[object Object], why Not "jack"?


Changed to this,

Copy the codeThe code is as follows:


//There is a return value Function fun
function fun() {
alert('I was called');
return {name:'jack'};
}

var c = new fun();
alert(c.name);//jack, returned normally again


Okay, 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, function , array and other object types, the object, function, array will be returned.

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.


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