The construction function constructor of JavaScript refinement and the _javascript techniques of constructor attributes

Source: Internet
Author: User
Tags function prototype

In addition to creating objects, the constructor (constructor) does another useful thing-automatically sets the prototype object (prototype objects) for the new object created. The prototype object is stored in the Constructorfunction.prototype property.

For example, we rewrite the previous example, using the constructor to create the object "B" and "C", then the object "a" acts as the "Foo.prototype" role:

constructor function
Foo (y) {
 //constructor will create an object in a specific pattern: the object being created will have a "Y" attribute
 this.y = y;
}
"Foo.prototype" holds a prototype reference to a new object
//So we can use it to define inheritance and shared properties or methods
//So, as with the previous example, we have the following code:
//Inheritance property "X"
foo.prototype.x =;
Inheritance method "Calculate"
Foo.prototype.calculate = function (z) {return
 this.x + This.y + z;
};
Create "B" and "C"
var b = new Foo () using Foo mode;
var c = new Foo ();
Invoke Inherited Methods
b.calculate ();// 
c.calculate ();////Let's 
see if we've used the expected attribute
Console.log (
 b.__proto_ _ = = = Foo.prototype,//true
 c.__proto__ = = = Foo.prototype,//True
 //Foo.prototype "automatically creates a special attribute" Constructor "
 //point to a constructor itself
 //instance" B "and" C "can be identified by 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 preceding code can be expressed as the following relationship:

The relationship between constructors and objects

As you can see from the above diagram, each object has a prototype. Constructor Foo also has its own __proto__, which is Function.prototype, and Function.prototype's __proto__ points to Object.prototype. Again, Foo.prototype is just an explicit property, which is the __proto__ property of B and C.

The complete and detailed explanation of this question has two parts:

Object-oriented Programming-General Theory (OOP). The general theory describes different object-oriented paradigms and styles (OOP Paradigms and Stylistics) and comparisons to ECMAScript.

Object Oriented Programming. ECMAScript implementation (OOP. ECMAScript implementation), which specifically describes object-oriented programming in ECMAScript.
Now that we know the basic principle of object, let's take a look at the program execution environment inside ECMAScript [Runtime programs execution]. This is what is commonly known as the "execution context stack" [execution contexts stack]. Each element can be abstracted as object. You may have found, yes, in the ECMAScript, almost everywhere you can see the object figure.

Here's a description of the JavaScript constructor attribute

The constructor property of the object is used to return the function that created the object, which is what we often call the constructor.

In JavaScript, each object with a prototype automatically gets the constructor attribute. In addition to some special objects such as arguments, enumerator, Error, Global, Math, RegExp, Regular expression, all other JavaScript built-in objects have the constructor attribute. For example: Array, Boolean, Date, Function, Number, Object, string, and so on. This property is supported by all major browsers.

Grammar

Object.constructor

return value

The constructor property of the object returns a reference to the function that created the object.

Example & Description

[native code] in the following code indicates that this is the underlying internal code implementation of JavaScript and cannot display code details.

Strings: String () var str = "John"; 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//digit: 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 objects: Object () var o = {"name": "John"}; Document.writeln (O.constructor); Function object () {[native code]} document.writeln (O.constructor = = = object); True//Custom functions: Function () function foo () {alert ("Codeplayer");} document.writeln (Foo.constructor); function function () {[nativeCode]} 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
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.