动态创建方法原型的优点:
1)属性不会共享
2)方法只产生一个
3)方法和属性一起定义
例子:
function DynaPrototype() {
this.name = "test";
if(typeof DynaPrototype.flag == "undefined") {
alert("execute...");
DynaPrototype.prototype.execute = function() {
alert("name: " + this.name);
};
DynaPrototype.flag = true;
}
}
说明:
1)“execute...”对话框只显示一次。
2)通过flag变量对方法的初始化进行控制。即当方法定义之后flag值为“true”,方法便不再定义。
1)属性不会共享
2)方法只产生一个
3)方法和属性一起定义
例子:
function DynaPrototype() {
this.name = "test";
if(typeof DynaPrototype.flag == "undefined") {
alert("execute...");
DynaPrototype.prototype.execute = function() {
alert("name: " + this.name);
};
DynaPrototype.flag = true;
}
}
说明:
1)“execute...”对话框只显示一次。
2)通过flag变量对方法的初始化进行控制。即当方法定义之后flag值为“true”,方法便不再定义。