ITeye博客

浅谈 javascript 对象的继承方法

携手走天涯 · 2012-12-20 23:59 · 编程语言 · JavaScript · Markdown

1.对象冒充 function Father(name) { this.name = name; this.sayName = function() { alert(this.name); } }

function Child(name,age) { this.method=Father; this.method(name); delete this.method; this.age = age; this.sayAge = function() { alert(this.age); } }

2.用call方法实现

function Father(name) { this.name = name; this.getName =function () { return this.name; } }

function Child(name,password) { Father.call(this,name); this.password = password; this.getPassword=function() { return this.password; } }

3.使用原型链方式 function Father() { }

Father.prototype.name = "zhangsan"; Father.prototype.getName=function() { return this.name; }

function Child() { } Child.prototype = new Father(); Child.prototype.password = "134"; Child.prototype.getPassword=function() { return this.password; }

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。