The "This" parameter in JavaScript

Source: Internet
Author: User

When a function is called, there is an implicit parameter----the This----is also assigned, in addition to the parameters that are explicitly declared when the function definition is assigned.

This parameter points to an object that is implicitly related to the function call, which is referred to as the context of the function (functions context), or called the invocation context (invocation contexts), and may feel clearer.

When called as a function

Function Ninja () {};

Ninja ();

In this case, the function context is the global context----the Window object.

When called as a function, the function is defined in the Window object, but in principle it is the same as the function being called as a method call.

When called as a method

var o = {};

O.whatever = function () {};

O.whatever ();

Points to the owning object.

Called as a constructor

When the constructor is called, the following behavior occurs:

A new empty object is created

The object is passed as a parameter to the constructor and becomes the function context of the constructor function

The new object is returned as the return value of the constructor function

Through the Apply () and call () methods

Juggle.apply (NINJA1, [1,2,3,4]);

Juggle.call (NINJA2, 5,6,7,8);

The context in which a function call can be explicitly defined

  

References: <secrets of the JavaScript ninja>

The "This" parameter in JavaScript

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.