This example in JavaScript learning and how it works

Source: Internet
Author: User
Tags bind

  This article mainly introduces JavaScript in this sample learning and working principles, we refer to the use of the bar

How this works   if a function is invoked as a method of an object, this will be assigned as the object.   Code as follows: var parent = {    method:function () {        Console.log (this);   &nbs P } };   Parent.method (); <-Parent       Note that this behavior is very "fragile", and if you get a reference to a method and call it, then the value of this is not parent, but the window global object. This is confusing to most developers.   Code as follows: Thisclowncar (); <-Window   Changes this   calls,. Apply and. Bind methods are used to manipulate the way functions are invoked, helping us to define the value of this and the parameter values passed to the function.   Function.prototype.call can have any number of arguments, the first parameter is assigned to this, and the rest is passed to the calling function. The code is as follows: Array.prototype.slice.call ([1, 2, 3], 1, 2)//<-[2]   Function.prototype.apply behaves like. Call, but the argument it passes to a function is a number Group, rather than any parameter.   String.prototype.split.apply (' 13.12.02 ', ['. '])//<-[', ', ', ']     Function.prototype.bind Creates a special function that will always use the parameter passed to. Bind as the value of this, as well as the ability to allocate part of the parameters to create the Curride version of the original function.   Code as follows: var arr = [1, 2]; var add = Array.prototype.push.bind (arr, 3);  //effectively the same as Arr.push (3) Add ();  //effectively the same as aRr.push (3, 4) Add (4);   Console.log (arr); <-[1, 2, 3, 3, 4]     in the scope chain of this   in the following example, this will not remain unchanged in the scope chain. This is a flaw in the rules and often creates confusion for amateur developers.   Code as follows: function scoping () {  Console.log (this);     return function () {    Console.log t His);  }; }   Scoping () (); <-window//<-window     There is a common way to create a local variable that holds a reference to this and cannot have the same variable in the child scope. A variable with the same name in a child scope overrides the reference to this in the parent scope. http://www.cnblogs.com/sosoft/      code is as follows: function retaining () {  var self = this;     RET Urn function () {    console.log (self);  }; }   Retaining () (); <-Window     Unless you really want to use this for both the parent scope and the current this value, I prefer the method that is used for some inexplicable reason. The bind function. This can be used to specify the this of the parent scope to the child scope.     Code is as follows: function bound () {  return function () {    Console.log (this),  }.bind (this);}   Bound () (); <-Window  
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.