JS function call mode

Source: Internet
Author: User

1, as a function call

function MyFunction (A, b) {    return A * b;} MyFunction (10, 2);

This is a common method of function invocation in JS, but it is not a good programming habit. Global variables, methods, or functions that are prone to naming conflicts.

The function is called as a global object , which makes the value of this a global object. Using the Window object as a variable can easily cause a program to crash.

2. Method invocation as an object

var myObject = {    firstName:"John",    "Doe",    function  () {        returnthis. lastName;    

The function acts as an object method call , which causes the value of this to be the object itself, which points to the MyObject object.

3. Call with constructor function

If the new keyword is used before a function call, the constructor is called.

This looks like the creation of a new function, but the JavaScript function is actually a re-created object :

// constructor Function: function myFunction (arg1, arg2) {    this. firstName = arg1;      this. LastName  = arg2;} // This creates a new object var New myFunction ("John", "Doe"); x.firstname;   

The call to the constructor creates a new object. The new object inherits the properties and methods of the constructor.

The This keyword does not have any value in the constructor. The value of this is created when the object is instantiated (new object) when the function is called.

4. Call function as Function method

In JavaScript, a function is an object. The JavaScript function has its properties and methods.

Call () and apply () are predefined function methods. Two methods can be used to call a function, and the first parameter of two methods must be the object itself.

function MyFunction (A, b) {    return A *
MyArray = [10,2];
Myfunction.apply (MyObject, MyArray);

Notes on Call and apply:

All two methods use the object itself as the first parameter. The difference between the two is the second argument: Apply passes in an array of parameters, which is the combination of multiple parameters into an array, and call is passed in as a call parameter (starting with the second argument).

Under JavaScript Strict mode (strict mode), when a function is called, the first parameter becomes the value of this, even if the parameter is not an object.

In JavaScript's non-strict mode, if the value of the first parameter is null or undefined, it uses the global object override. non-strict

You can set the value of this with the call () or the Apply () method, and it is called as a new method of an existing object.

JS function call mode

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.