Closures, prototype chains

Source: Internet
Author: User

Closed Package

In function definition, the scope property of the function object refers to the active object of the execution context that is generated at run time by the upper-level function that contains the function, because of the interdependence between object properties, which causes the activity variable that contains the scope property of the function to point to. "Cannot release" the active variable it contains when the previous function "executes" and will "release execution context".

Own understanding: The generation of closures must first have an address referenced by the

Relationship of functions to objects
    1. A function is a kind of object
    2. object is created by a function
Prototype prototypes
    1. Prototype is a property of a function
    2. Each constructor has a prototype property
    3. The value of prototype is actually an object
      • This object defaults to only one constructor property, and points to the function itself
Differences between properties in a constructor and properties in a prototype
    1. Defining a property in a prototype is smaller than defining the memory consumed in the constructor, because there is only one prototype in memory, and the behavior that is written in the prototype can be shared by all instances. Instantiation is not duplicated in memory. For no particular reason, attributes are generally written to the class, and the behavior is written into the prototype.
    2. Properties are defined using prototypes, in fact, properties in different objects are shared, meaning that properties are modified for any one of them, and the properties of other objects change as they point to the same address, sharing the same property
    3. The properties and methods defined in the constructor are higher precedence than those defined in the prototype. If you define properties and methods of the same name, the constructor overrides the one in the prototype.
Implicit prototype (object) Implicit prototype: _proto_ is an attribute in an object, each object has a _proto_ property, also known as an implicit prototype. and point to the prototype that created the object
    1. The custom function is essentially created by the object function, so its _proto_ points to Object.prototype
    2. Object.prototype _proto_ points to null
Prototype chain: When accessing an object's properties, it is found in the basic attribute, and if it does not have the attribute, it will be looked up along the _proto_ chain. Execution context

Before the execution of a section of JS code, the browser has done some preparatory work, including the declaration of variables, variable assignment is performed at the time of the assignment statement.

There are three steps to preparing your work:
    1. Variable: Declaration of variable, default assignment is undefined
    2. This: Assign value
    3. function declarations: Assigning values
The preparation of these three kinds of data is called "execution context" or "execution context";
函数每被调用一次,都会产生一个新的执行上下文环境,因为不同的调用可能会有不同的参数。函数定义的时候就应经确定了函数体内部变量的作用域
Execution context Stack

When executing the global code, a global context is generated, each time a function is called, and a function context is generated, and when the function call is complete, the context and the data in it are destroyed. And then back to the global context environment.

There is only one execution context environment that is active. (The process of pressing and stacking)

Scope and execution context

In addition to the global scope, each function creates its own scope. The scope is determined when the function is defined, not when it is called. Scope is just a "site", an abstract concept, where there is no variable, to get the value of a variable through the execution context of the scope, under the same scope, different calls produce different execution contexts, and then different values are generated.

Free variables

A variable used in a scope is not declared in it (that is, declared in another scope), at which point the variable is a free variable (also known as an active variable) for this scope.

Scope chain
    1. Finds x in the current scope first, if any, gets and ends, if not, continues;
    2. If the current scope is a global scope, the proof x is undefined, ends; otherwise continues;
    3. The scope of the function is created as the current scope;
    4. Jump to the first step.
Inherited
  1. Object Impersonation

        function Person(name, age) {           this.name = name;           this.age = age;           this.sayName = function() {               console.log(this.name);           };       }       function Student(name, age) {           this.obj = Person;           this.obj(name, age);           delete this.obj;           this.study = function() {               console.log("study");           }       }       var stu1 = new Student("zhangsan", 21);       console.log(stu1.name, stu1.age);       stu1.sayName();       stu1.study();
  2. The way of the prototype chain

    function Person(name, age) {   this.name = name;   this.age = age;   this.sayName = function () {       console.log(this.name);   };}function Student(name, age) {   this.constructor(name, age);}Student.prototype = new Person();var stu1 = new Student("zhangsan", 21);console.log(stu1.name, stu1.age);
  3. Call and Apply Methods

            function Student(name, age) {//            Person.call(this, name, age);            Person.apply(this, [name, age]);        }        var stu = new Student("zhangsan", 21);        console.log(stu.name, stu.age);
  4. Blending mode

    function Person(name, age) {    this.name = name;    this.age = age;}Person.prototype.sayName = function() {    console.log(this.name);};function Student(name, age) {    Person.call(this, name, age);}Student.prototype = new Person();

Closures, prototype chains

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.