JS中prototype的实例

来源:互联网 发布:都玩网络怎么样 编辑:程序博客网 时间:2024/04/25 19:19

1、可以在类型上使用proptotype来为类型添加行为。这些行为只能在类型的实例上体现

JS中允许的类型有Array, Boolean, Date, Enumerator, Error, Function, Number, Object, RegExp, String

 Object.prototype.Property = 1;Object.prototype.Method = function (){    alert(1);} var obj = new Object();alert(obj.Property);obj.Method();



2、在实例上不能使用prototype,否则发生编译错误


var obj = new Object(); obj.prototype.Property = 1; //Error//Errorobj.prototype.Method = function(){    alert(1);}



3、可以为类型定义“静态”的属性和方法,直接在类型上调用即可


Object.Property = 1;Object.Method = function(){    alert(1);}alert(Object.Property);Object.Method();



4、可以在外部使用prototype为自定义的类型添加属性和方法


function Aclass(){this.Property = 1;this.Method = function(){    alert(1);}}Aclass.prototype.Property2 = 2;Aclass.prototype.Method2 = function{    alert(2);}var obj = new Aclass();alert(obj.Property2);obj.Method2();



参考资料:  JS中prototype    http://www.studyofnet.com/news/556.html


0 0
原创粉丝点击