Ruan Yifeng weblog felt

http://www.ruanyifeng.com/blog/javascript/

1. Closure

I understand looked after, the closure is the function of the function, so why write it, usually we can get the value of a global variable, and now I want to get the value inside the function, how to obtain it is through writing function a function (this function can fetch parent function), then return out this function, the function can get inside the external variable ah, closures memory consumption, in essence, is the closure and the internal functions functions external connecting bridge.

  var name = "The Window";

  var object = {
    name : "My Object",

    getNameFunc : function(){
      return function(){
        return this.name;
      };

    }

  };

  alert(object.getNameFunc()()); //The Window

  Why is the window, because the

this is based on the object at run-time execution environment binding function, global function, this = window, when the function is invoked as a method of an object, this is equal to this object.

But the anonymous function execution environment is global in nature, where getNameFunc methods return an anonymous function, this anonymous function in this point window

The solution is still there, we can pass to the outside of the scope of this anonymous function

  var name = "The Window";

  var object = {
    name : "My Object",

    getNameFunc : function(){
      var that = this;
      return function(){
        return that.name;
      };

    }

  };

  alert(object.getNameFunc()());My Object

We learned the anonymous function execution environment is a pointer to the global window

2.js languages ​​dross

Analyzing the JavaScript == implied by a variety of implicit conversion pits for this design, is not the best way to avoid the use, there is no need to pay for dross language.

== and! = Automatically typecast

‘true’ ==true -->false

Reference rules https://www.cnblogs.com/wl521/p/7423852.html

This is the pot js design language, in order to avoid this pot, later wrote the project with all the === and! == had equal type and value

3.

Guess you like

Origin www.cnblogs.com/myfirstboke/p/11357159.html