JS Basics: The point of this and the role of call and apply

Source: Internet
Author: User

Tag: Type bind cannot be implemented with UNC rap AC property variable

The point of this

In the practical application, the point of this can not be determined at the time of function definition, but is determined when the function is executed, according to the environment of the execution is roughly divided into the following 3 kinds:

1. When the function is called as a normal function, this points to the global object

2. When a function is called as a method of an object, this points to the object

3. When the function is called as a constructor, this points to the newly created object

Example one:

Window.name = ' myname '; function GetName () {    console.log (this// output myname

Example two:

var boy = {    ' Bob ',    function() {        Console.log (this . Name);     // output Bob

Example three:

function Boy (name) {    this. Name = name;}  var New Boy (' Bob '// output Bob

For example three, there is also a special case, that is, when the constructor returns an object by "return", the final result of this operation is to return the object, not the newly created object, so this is not a use in this case.

Example four:

function Boy (name) {    this. Name = name;      return  // Returns an object          ' Jack '    }}varnew Boy (' Bob '// output Jack

Example five:

function Boy (name) {    this. Name = name;      return // return non-object }varnew Boy (' Bob '// output Bob

The role of call and apply

Apply accepts two parameters, the first parameter specifies the pointer to this in the function body, and the second parameter is an array or an array of classes that pass the argument list of the called function.

Example one:

function GetInfo () {    Console.log (this. name+ ' like ' +arguments[0]+ ' and ' +arguments[1]);}  var boy1 = {    ' Bob ',    }getinfo.apply (boy1,[// output Bob Like sing and swimming

The number of call incoming parameters is not fixed, the same as apply, the first parameter is also used to specify the direction of this in the function body, starting from the second argument, each parameter is sequentially passed into the called function.

Example two:

function GetInfo () {    Console.log (this. name+ ' like ' +arguments[0]+ ' and ' +arguments[1]);}  var boy1 = {    ' Bob ',    }getinfo.call (boy1,// output Bob Like sing and shopping

In addition, most advanced browsers also implement the Bind method, which differs from call and apply in that bind simply changes the point of this inside the function but does not execute immediately and you need to show it.

Example three: Simulating the browser's bind method

Function.prototype.bind =function(obj) {varSelf = This; return function(){        returnself.apply (obj,arguments); }};varobj ={name:' Bob ', Age:12};varFunc =function() {Console.log ( This. name+ ' like ' +arguments[0]+ ' and ' +arguments[1]);}. Bind (obj); Func (' Sing ', ' shopping ');

The missing this

In some cases the point of this is lost, and we need to use call, apply, and bind to change the point of this.

Example one: When the "GetName" method is called as a property of the "Boy" object, this points to the "boy" object when another variable refers to the "GetName" method because it is called as a normal function, so this is a pointer to the Global object window

var boy = {    ' Bob ',    function() {        Console.log (this . Name);     // output Bob var getboyname =// output Undefind

Example two: Even if a function is defined inside a function, if it is called as a normal object, this also points to the Window object

var boy1 = {    ' Bob ',    #,    function() {        Console.log (  this. Name);         function Getage () {            Console.log (this. Age);        }        Getage ();     // Bob                // Undefind

JS Basics: The point of this and the role of call and apply

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.