« 小田 | 首页 | P »
个人主页: http://yqjun.tk
在 Javascript面向对象编程(二):构造函数的继承 留言:
你好,我想问一下,关于第5种继承方法。子类继承了父类的数据和prototype方法,但之后子类貌似无法再使用自己的prototype方法了,因为prototype方法在extend()函数中已经被改写了。 莫非是父类使用prototype方法后,子类就无法使用?难道这种情况下,子类只能通过this添加新方法?这样的话我new多个子类就会造成代码重复吧?求解答。 function Parent() { this.a = 'a'; } Parent.prototype = { print: function() { alert(this.a); } } function Child() { Parent.apply(this, arguments); } Child.prototype = { print2: function() { alert('d'); } } extend(Child, Parent); var obj = new Child(); obj.print();//输出'a' obj.print2();//报错,找不到function
2011-10-31 14:14:37
留言(1 条)
在 Javascript面向对象编程(二):构造函数的继承 留言:
你好,我想问一下,关于第5种继承方法。子类继承了父类的数据和prototype方法,但之后子类貌似无法再使用自己的prototype方法了,因为prototype方法在extend()函数中已经被改写了。
莫非是父类使用prototype方法后,子类就无法使用?难道这种情况下,子类只能通过this添加新方法?这样的话我new多个子类就会造成代码重复吧?求解答。
function Parent() {
this.a = 'a';
}
Parent.prototype = {
print: function() {
alert(this.a);
}
}
function Child() {
Parent.apply(this, arguments);
}
Child.prototype = {
print2: function() {
alert('d');
}
}
extend(Child, Parent);
var obj = new Child();
obj.print();//输出'a'
obj.print2();//报错,找不到function
2011-10-31 14:14:37