Home >Web Front-end >JS Tutorial >How to call functions in javascript

How to call functions in javascript

醉折花枝作酒筹
醉折花枝作酒筹Original
2021-04-16 17:53:5715199browse

Method: 1. Call with the "function name (parameter, parameter)" statement; 2. Call as a method of the object, the syntax is "object name.Method name ()"; 3. Call with call or apply, The syntax is "function name.call|apply(parameter, parameter)"; 4. Use the new command, the syntax is "new function name (parameter, parameter)".

How to call functions in javascript

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

JavaScript provides 4 types of function calls: general function calls, method calls as objects, dynamic calls using call and apply, and indirect calls using new.

1. General form of function call

In the default state, the function will not be executed. Use parentheses () to activate and execute functions. Zero or more parameters can be included in parentheses, separated by commas.

In the following example, use parentheses to call the function, and then directly pass the return value into the function for the second operation, which can save two temporary variables.

function f(x,y) {  //定义函数
    return x * y;  //返回值
}
console.log(f(f(5,6), f(7,8)));  //返回1680。重复调用函数

2. Calling methods as objects

In JavaScript you can define functions as methods of objects.

The following example creates an object (myObject), which has two properties (firstName and lastName), and one method (fullName)

var myObject = {
    firstName:"John",
    lastName: "Doe",    
    fullName: function () {
        return this.firstName + " " + this.lastName;
        }
    }
        myObject.fullName();         // 返回 "John Doe"

3. Use call and apply dynamically Call

call and apply are the prototype methods of Function. They can bind a specific function as a method to the specified object and call it. The specific usage is as follows:

function.call(thisobj, args...)
function.apply(thisobj, [args])

function represents the function to be called; the parameter thisobj represents The binding object is the object referred to by this; the parameter args represents the parameters to be passed to the called function. The call method can receive multiple parameter lists, while apply can only receive an array or pseudo-class array, and the array elements will be passed to the called function as a parameter list.

The following example uses call to dynamically call function f, passes in parameter values ​​3 and 4, and returns the operation value.

function f(x,y) {  //定义求和函数
    return x + y;
}
console.log(f.call (null, 3, 4));  //返回7

The main functions of the all and apply methods are as follows:

  • Call the function.

  • Modify the this reference object in the function body.

  • is the object binding method.

  • Call methods of different types across restrictions.

4. Indirect call by new command

Use new command to instantiate objects. This is its main function, but in Functions are activated and run during object creation. Therefore, functions can be called indirectly using the new command.

The following example briefly demonstrates how to use the new command to display the passed parameter value on the console.

function (x,y) {  //定义函数
    console.log("x =" + x + ", y =" + y);
}
new f(3,4);

[Recommended learning: javascript advanced tutorial]

The above is the detailed content of How to call functions in javascript. For more information, please follow other related articles on the PHP Chinese website!

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