In addition to creating objects, constructor also does another useful thing-automatically creates
In addition to creating objects, constructor has also done another useful thing: prototype object is automatically set for the newly created object ). The prototype object is stored in the ConstructorFunction. prototype attribute.
For example, if we rewrite the previous example and use the constructor to create objects "B" and "c", then object "a" assumes the role "Foo. prototype:
// Constructor function Foo (y) {// constructor will create an object in a specific mode: the object to be created will have the "y" attribute this. y = y;} // "Foo. prototype "stores the prototype reference of the new object // so we can use it to define the inheritance and sharing attributes or methods. Therefore, like in the preceding example, we have the following code: // inherit the property "x" Foo. prototype. x = 10; // The Inheritance Method "calculate" Foo. prototype. calculate = function (z) {return this. x + this. y + z ;}; // use the foo mode to create "B" and "c" var B = new Foo (20); var c = new Foo (30 ); // call the inherited method B. calculate (30); // 60c. calculate (40); // 80 // Let's see if the expected property console is used. log (B. _ proto _ = Foo. prototype, // true c. _ proto _ = Foo. prototype, // true // "Foo. prototype "automatically creates a special attribute" constructor "// points to the constructor itself of a // instance" B "and" c "can be found through authorization and used to detect its own constructor B. constructor = Foo, // true c. constructor = Foo, // true Foo. prototype. constructor = Foo // true B. calculate = B. _ proto __. calculate, // true B. _ proto __. calculate = Foo. prototype. calculate // true );
The above code can be expressed as the following link:
Relationships between constructors and objects
The preceding figure shows that each object has a prototype. the constructor Foo also has its own _ proto __, that is, Function. prototype, while Function. prototype _ proto _ points to Object. prototype. reapply, Foo. prototype is only an explicit attribute, that is, the _ proto _ attribute of B and c.
The complete and detailed explanation of this question includes two parts:
- Object-Oriented Programming-general theory (OOP. The general theory) describes different object-oriented paradigms and stylistics and compares them with ECMAScript.
- Object-Oriented Programming. ECMAScript implementation (OOP. ECMAScript implementation) specifically describes Object-Oriented Programming in ECMAScript.
Now that we know the basic object principle, let's take a look at the program execution environment [runtime program execution] In ECMAScript. this is commonly referred to as "execution context stack" [execution context stack]. Each element can be abstracted as an object. You may have discovered that, right. In ECMAScript, You can see objects almost everywhere.
Additional reading
The topic list of this article is as follows:
- How should we understand the working principle of the JavaScript engine?
- JavaScript exploration: the importance of writing maintainable code
- JavaScript exploration: exercise caution when using global variables
- JavaScript exploration: var pre-parsing and side effects
- JavaScript exploration: for Loop (for Loops)
- JavaScript exploration: for-in loop (for-in Loops)
- Exploring JavaScript: Prototypes is too powerful
- JavaScript: eval () is the devil"
- JavaScript exploration: Using parseInt () for Numerical Conversion
- Exploring JavaScript: Basic coding specifications
- JavaScript exploration: function declaration and function expression
- JavaScript exploration: Name function expressions
- JavaScript: function name in the debugger
- JavaScript: JScript Bug
- JavaScript exploration: Memory Management of JScript
- Exploring JavaScript: SpiderMonkey's quirks
- JavaScript exploration: an alternative solution to naming function expressions
- JavaScript exploration: Object
- JavaScript exploration: Prototype chain
- JavaScript exploration: Constructor
- JavaScript probing: executable context Stack
- Execution context 1: Variable object and activity object
- Execution context 2: Scope chain Scope Chains
- Execution context 3: Closure Closures
- Execution context 4: This pointer
- Exploring JavaScript: Powerful prototype and prototype chain
- JavaScript Functions 1: function declaration
- JavaScript function 2: function expressions
- JavaScript function 3: function expressions in a group
- JavaScript function 4: function Constructor
- JavaScript variable object 1: VO Declaration
- JavaScript variable object 2: VO in different execution contexts
- JavaScript variable object 3: two stages of execution Context
- JavaScript variable object IV: Variables
- Property of the JavaScript variable object __parent _
- JavaScript scope chain 1: Scope chain Definition
- JavaScript scope chain 2: function Lifecycle
- JavaScript scope chain 3: Scope chain features
- JavaScript closure 1: Introduction to closures
- JavaScript closure 2: Implementation of closure
- JavaScript closure 3: Closure usage
This article is available at http://www.nowamagic.net/librarys/veda/detail/1642.