Features of constructors and various inheritance methods

Source: Internet
Author: User

---always returns an object when a function is called with the new operator

1. When you call any function with new, it happens as follows:

2. The background automatically creates an "empty" object, which is referenced by this object; var this={};//pseudo-code

3. You can add properties to this

4. Implicitly returns this at the end of the function

However, you can also return different objects by using the following steps:

function Dog () {var odog=new dog ();

var nothis={noname: "Any"}; Odog.name; Undefined

This.name= "Fod"; Odog.noname.name;//any returns a custom object (not this)

Return nothis;//covers the

}

When you use new, you can return a custom object (not this), and if a non-object (scalar value) is returned, it will cause the return value to be ignored and will eventually get this. For example:

function Dog () { var odog=new dog ();

This.name= "Fod"; Odog.name; Fod

return 1;

}

When referencing a constructor, you may forget to add new and we can use this function to return to the constructor function.

function Dog () {

if (! ( This instance Dog)) {

return new Dog ();

}

}

----//Direct literal inherits the attribute of another direct literal

function extend (parent) {

var child={};

for (var name in Paret) {

if (Parent.hasownproperty (Naem)) {

child[name]=parent[name];

}

}

return child;

}

Temporary constructors

function Extend (obj) {

var f=function () {};

F.prototype=o;

return new F ();

}

function Extend (parents,child) {

var f=function () {};

F.prototype=parent.prototype;

Child.prototype=new F ();

Child.prototype.constructor=child;

Each object is connected to a prototype object and can inherit properties from it, so objects created by object literals are connected to the Object.prototype----JS Standard object

----The constructor is borrowed, the new object gets the tags members in the parent object:

function Parent () {

this.name=["CSS", "JS"];

}

parent.prototype.sayname=function () {console.log (this.name)};

var oparent=new Parent ();

function Child1 () {};

Child.prototype=oparent;

var ochild1=new Child1 ();

Ochild1.sayname ();

function Child2 () {//cannot inherit a property or method on a prototype

Parent.call (this);

}

var ochild2=new Child2 ();

Ochild2.sayname (); //uncaught TypeError:oChild2.sayName is not a function (...)

Console.log (Ochild1.hasownproperty ("name")); false

Console.log (Ochild2.hasownproperty ("name")); True

oChild1.name.push ("PHP");

OChild2.name.push ("Java"); Just a copy, you can't add a value to a parent's attribute

Console.log (Oparent.name); ["CSS", "JS", "PHP"]

------borrowing and setting up prototypes, both the properties of the parent object and the prototype properties can be

function Child () {

Parent.apply (this,arguments);

}

Child.prototype=new Parent ();

var ochild=new child ();

Ochild.sayname ();

Object-oriented: Var obj={}; object.defineproperty (obj, "name", {     writable:false|true ,//Whether you can change whether      ennumerable:false|true,//is enumerable      configurable : false|true,//can delete      value: ""  //set value  });   define multiple properties:  Object.defineproperties ();  object.create ();//The first parameter passed in is the prototype object of the created object isPrototypeOf (); Person.isprototypeof (Person1);//true|falseobject.getprototypeof () Gets the prototype of an instance  //object.getprototypeof (Person1 ); Person.prototypehasownprototype ()//detects whether a property exists in an instance, the method is inherited from Object in  operator  object.keys () accepts an object as a parameter, Returns an array of strings that contain all enumerable properties,  object.getownpropertynames ()//enumerations that are either enumerated or not   prototype, and workarounds     Inheritance: The implementation of inheritance is mainly dependent on the prototype chain, the essence is to rewrite the property prototype.   exists in all attribute methods in the inherited object instance, and also exists in the prototype with the active inheritance, because the prototype of the active inheriting object refers to the  sub instance of the instance of the inherited object constructor property points to the Sup object, because the original object prototype is overridden for the sake of- ->sub's prototype points to the prototype of the SUP, while the sub's prototype's constructor attribute points to the SUP.   Borrowing constructors://Only inherit properties and methods from an instance
function Child (name) {    Parent.call (this, name);}

Combination inheritance://Will inherit instance properties of the superclass two times, first integration into the instance, second inheritance into the prototype, overwritten in the prototype, and therefore with the subsequent combination of parasitic inheritance
function Child (name) {        Parent.call (this, name);} Child.prototype=new Parent ();

Prototype-Type Inheritance:
function Object (o) {    var f=function() {};    F.prototype=o;     return New

Parasitic inheritance:
function Createanother (Origin) {    var f=function() {};    F.prototype=o;     var clone=New  F ();    CLONE.ADDFN=function() {}; // The added function is not reusable and less efficient. 

Parasitic combination inheritance:
var sub=function() {    Sup.call (this);} function Extend (child,parent) {    var f=function() {};    F.prototype=parent.prototype;    Child.prototype=new  F ();}

Features of constructors and various inheritance methods

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.