js面向对象-动态原型模式
文章目录
动态原型模式(不能使用对象字面量重写原型)
把所有信息封装到构造函数中,通过检查某个应该存在的方法是否有效,来决定是否初始化原型。
function Person(name, age, job) {
//属性
this.name = name;
this.age = age;
this.job = job;
// 方法
if (typeof this.whatJob != "function") {
Person.prototype.whatJob = function () {
alert(this.job);
};
}
}
var friend = new Person("wheeler", 25, "Software Engineer");
friend.whatJob();

评论
登录后参与技术讨论,分享你的经验与见解。
登录 / 注册暂无评论