JavaScript Ninja Cheats-Chapter III notes (Basis of function) __ function

Source: Internet
Author: User
Tags assert

JavaScript is holding the cheat note function call passed two implicit parameters Argument,this

Implicit: means that these parameters do not display columns in the function signature, but they are silently passed to the function in the scope of the function.

Inside functions They can be used like cards to display named arguments

If you're debugging in your browser, you're going to happen they're real. arguments parameters in objects

He's just an array of classes, just having some properties, you can get the array form to get the first argument (arguments[0), or for loop traversal, but he's not the array this argument

This is called the context of the caller

And the function calls several ways. Called as a function, is the simplest form. Called as a method, invoked on an object, and supports object-oriented programming. Called as a constructor, creating a new object. Calling through the Apply () or call () method is complex, and we'll talk about calling it as a function in the simplest form when we encounter it.

In general, this is also the use of

Such as:

Function Ninja () {};
Ninja ();

And their this, that is, window is called as a method

And one is called as a method in a function, this time this is the function

Such as:

var creep=function ninja () {};

var sneak={
    skulk:creep
}

sneak.skulk ()//skulk This is the Sneak object

called as a constructor

When the constructor is invoked, the following behavior occurs:

Create a new empty object
The object passed to the constructor is the this argument, which becomes the constructor's function context
If no return value is displayed, the newly created object is returned as a constructor

Called as a constructor, this is the object

function Ninja () {
  this.skulk=function () {return this
        ;
    }
}

var ninja1=new Ninja ();

ASSERT (Ninja1.skulk () ===ninja1);/Through
called using the Apply () and call () methods

In the function call, JavaScript provides us with a way to show that any object is specified as its function context, and every function of JavaScript has the Apply () and call () method

Apply () pass in two parameters:
Object of the function context
An array of function arguments

The call () method uses the same method, except that the argument passed to the function is a parameter instead of a list, not a single array

Example:

function juggle () {
 var result=0;
 for (Var n=0;n<arguments.length;n++) {
    result+=argument[n];
 }
 This.result=result;
}

var ninja1={};
var ninja2={};

Juggle.apply (ninja1,[1,2,3,4]);
Juggle.call (ninja2,1,2,3,4);

ASSERT (ninja1.result===10);/through

assert (ninja2.result===26);//Through

As you can see from the final test: the conclusion is correct.

NINJA1 and ninja2 the context.

The juggle method also succeeds in adding a result to the context

Of course, the final calculation is the same.

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.