原型中的方法是共享的,如果每次创建构造函数时不必在重新在原型中添加方法
function Person(name,age){ this.name = name; this.age = age; //方法 if(typeof this.sayName != "function") { Person.prototype.sayName = function(){ alert(); }; }}
这样在new Person时 sayName方法就不必每次都重新创建。
本文共 304 字,大约阅读时间需要 1 分钟。
原型中的方法是共享的,如果每次创建构造函数时不必在重新在原型中添加方法
function Person(name,age){ this.name = name; this.age = age; //方法 if(typeof this.sayName != "function") { Person.prototype.sayName = function(){ alert(); }; }}
这样在new Person时 sayName方法就不必每次都重新创建。
转载于:https://www.cnblogs.com/lcw5945/p/4142477.html