Defines a custom object constructor for a class (object)

Source: Internet
Author: User

object creation and inheritance object creation refers to a custom object constructor that is used to bulk create objects that have common property methods. For example, to create a human object constructor, and then instantiate xiaoming, small red inheritance refers to the inheritance of properties and methods between two reference type objects, using prototype because the instance retains a pointer to the constructor prototype, prototype retains a pointer to the constructor. So if you point the prototype of a constructor to another instance of an object type, then it inherits the pointer to the constructor prototype the instance and its pointer to the constructor function. 1, the oldest Factory mode, is to create a new object inside, and then configure the property, return this object, the problem is that the object type cannot be judged (instanceof):function Createperson (name, age, job) {
var o = new Object (); o.name = name; o.age = age; o.job = job; o.sayname = function () { alert (this.name);  }; return o;
}var person1 = Createperson ("Nicholas", "Software Engineer");var person2 = Createperson ("Greg", "Doctor"); 2, the constructor mode, you can use instanceof to judge, the problem is that each instance will create the same method, useless public:function person (name, age, job) {this.name = name;this.age = age;this.job = job;this.sayname = function () {alert (this.name); };}var person1 = new Person ("Nicholas", "Software Engineer");var person2 = new Person ("Greg", "Doctor");When a function is called by a new, the function is changed, and new is treated as a method, similar to the following: Function.method (' new ', function () {
var that = object.create (This.prototype); Create a new object that inherits from the constructor ' s prototype.
var other = this.apply (that, arguments); Invoke the constructor, binding–this-to the new object.
Return (typeof other = = = ' object ' && other) | |            that; If its return value is ' t ' an object, substitute the new object.
}); Forget new, this point is a global variable, to avoid this problem, add one more layer of Detection (SCOPE-SAFE): function Car (scolor) {if (this instanceof Car) {
This.color = Scolor;    }else {return new Car (Scolor); The problem with this is to close the context in which the constructor can be called: function Polygon (sides) {
if (this instanceof Polygon) {this.sides = Sides;this.getarea = function () {return 0;};} else {return new Polygon (SID ES);}
}function Rectangle (width, height) {
Polygon.call (this, 2); The call here polygon returns a new polygon instance, so this is not expanded this.width = Width;this.height = Height;this.getarea = function () {return This.width * this.height;};
}rectangle.prototype = new Polygon (); If you do not have this sentence will not be 2, but undefinedvar rect = new Rectangle (5, ten); alert (rect.sides); 2 3, prototype mode, the problem is that all properties are common, and the initialized value cannot be passed: function person () {} Person.prototype.name = "Nicholas"; Person.prototype.age = 29; Person.prototype.job = "Software Engineer"; Person.prototype.sayName = function () {alert (this.name);};                                       var person1 = new Person ();p erson1.sayname ();                                        "Nicholas" var person2 = new Person ();p erson2.sayname (); "Nicholas" alert (person1.sayname = = Person2.sayname);//true4, mixed constructor mode and prototype mode, take the respective advantages: function person (name, age, Job) {this.name = name; this.age = age; this.job = job; This.frien ds = ["Shelby", "Court"];} Person.prototype = {Constructor:person, sayname:function () {alert (this.name);}}; 5. Dynamic Prototype Pattern:function person (name, age, job) {this.name = name;this.age = age;this.job = job; if (typeof this.sayname! = "function") {Person.prototype.sayName = function () {alert (this.name);    };  }}var friend = new Person ("Nicholas", "Software Engineer");6. Parasitic Constructor Pattern:function Specialarray () {var values = new Array ();values.push.apply (values, arguments);values.topipedstring = function () {return This.join ("|"); }; return values;}var colors = new Specialarray ("Red", "Blue", "green");alert (colors.topipedstring ());//"Red|blue|green"7, durable Constructor pattern, this pattern instance and constructor prototype does not matter, cannot use instanceof:function person (name, age, job) {//create the object to returnvar o = new Object ();//optional:define Private Variables/functions here//attach Methodso.sayname = function () {alert (name); };//return the Objectreturn o;}var friend = person ("Nicholas", "Software Engineer");friend.sayname ();//"Nicholas"

Defines a custom object constructor for a class (object)

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.