JavaScript prototypes inherit _js object-oriented

Source: Internet
Author: User
Object.prototype
JavaScript is inherited based on prototypes, and any object has a prototype attribute. Object.prototype is the root of all objects and cannot be changed.
Copy Code code as follows:

Object.prototype=null;
alert (object.prototype);//[object Object]

Object and Object.prototype
Object inherits from Object.prototype, adds an attribute to the Object.prototype, and also reacts to object. Such as:
Copy Code code as follows:

Object.prototype.namestr= "Object prototype";
Object.prototype.getname=function () {return this.namestr};
Alert (Object.getname ());//object Prototype

Function.prototype and Object.prototype
Since Object.prototype is the root of all things, Function.prototype also inherits all the attributes of Object.prototype. Such as:
Copy Code code as follows:

Object.prototype.namestr= "Object prototype";
Object.prototype.getname=function () {return this.namestr};
Alert (Function.prototype.getName ());//object prototype


Object/function/string/number/boolean/array and date
Object/function/string/number/boolean/array and date are functions, and functions are inherited from Function.prototype, So changing the function.prototype will affect Object/function/string/number/boolean/array and date. Such as:
Copy Code code as follows:

Function.prototype.inittype= ' Function Type ';
Function.prototype.gettype=function () {return this.inittype};
Alert (Object.GetType ());//function Type
Alert (Date.gettype ());//function Type
Alert (Number.gettype ());//function Type
Alert (String.gettype ());//function Type
Alert (Boolean.gettype ());//function Type
Alert (Array.gettype ());//function Type

The same function.prototype will be influenced by the Object.prototype to pass on to its next level. Such as:
Copy Code code as follows:

Object.prototype.namestr= "Object prototype";
Object.prototype.getname=function () {return this.namestr};
Alert (Function.prototype.getName ());//object prototype
Alert (Array.getname ());//object Prototype

Copy Code code as follows:

Alert (Boolean.prototype.getName ());//object prototype

Array/array.prototype and Function.prototype/object.prototype

Array is a function object, Affected by Function.prototype, and Array.prototype is not a function object, is not affected by the Function.prototype, but all objects are affected by Object.prototype, so Array.prototype will also be affected by obje The effect of Ct.prototype. Such as:
Copy Code code as follows:

Object.prototype.namestr= "Object prototype";
Object.prototype.getname=function () {return this.namestr};
//alert (Function.prototype.getName ());//object prototype
//alert (Boolean.prototype.getName ());//object Prototype
Function.prototype.initfun=function () {
return ' Function.prototype.initFun ';
}
Alert (Array.initfun ());//function.prototype.initfun
var arr=[' A ', ' B '];
Alert (Arr.getname ());//object Prototype
Alert (Arr.initfun ());//error:arr.initfun is not a function
alert ( Arr.initfun);//undefined

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.