The constructor attribute of the object is used to return the function for creating the object, which is also known as the constructor. In addition to the object creation, the constructor) another useful thing is that prototypeobject is automatically set for the newly created object. In addition to creating an object, constructor) another useful thing is that prototype object is automatically set for the 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 =; // The inherited 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 (); var c = new Foo (); // call the inherited method B. calculate (); // c. calculate (); // 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.
The following describes the attributes of JavaScript constructor.
The constructor attribute of the object is used to return the function for creating the object, which is also known as the constructor.
In JavaScript, each prototype object automatically obtains the constructor attribute. Except for some special objects such as arguments, Enumerator, Error, Global, Math, RegExp, and Regular Expression, all other built-in JavaScript objects have the constructor attribute. For example, Array, Boolean, Date, Function, Number, Object, and String. All mainstream browsers support this attribute.
Syntax
Object. constructor
Return Value
The constructor attribute of the object returns a reference to the function used to create the object.
Example & Description
[Native code] in the following code indicates that this is the underlying internal code implementation of JavaScript and the code details cannot be displayed.
// String: String () var str = "zhangsan"; document. writeln (str. constructor); // function String () {[native code]} document. writeln (str. constructor = String); // true // Array: Array () var arr = [1, 2, 3]; document. writeln (arr. constructor); // function Array () {[native code]} document. writeln (arr. constructor = Array); // true // Number: Number () var num = 5; document. writeln (num. constructor); // function Number () {[native code]} document. writeln (num. constructor = Number); // true // custom object: Person () function Person () {this. name = "CodePlayer";} var p = new Person (); document. writeln (p. constructor); // function Person () {this. name = "CodePlayer";} document. writeln (p. constructor = Person); // true // JSON Object: Object () var o = {"name": "Zhang San"}; document. writeln (o. constructor); // function Object () {[native code]} document. writeln (o. constructor = Object); // true // User-Defined Function: function () Function foo () {alert ("CodePlayer");} document. writeln (foo. constructor); // function Function () {[native code]} document. writeln (foo. constructor = Function); // true // function prototype: bar () Function bar () {alert ("CodePlayer");} document. writeln (bar. prototype. constructor); // function bar () {alert ("CodePlayer");} document. writeln (bar. prototype. constructor === bar); // true