funcition Animal () {
}
Animal.prototype.type = "Animal"
function Cat (name,color) {
this.name = name;
this.color = color;
}
var f = function () { }
f.prototype = Animal.prototype
Cat.prototype = new f()
Cat.protope.constructor = Cat
复制代码
- 封装成方法
function extend(child,parent){
var f = function () {}
f.prototype = parent.prototype
child.prototype = new f()
child.prototype.constructor = child
child.uber = parent.prototype
}
复制代码
- 使用
entend(Cat,Animal)
var cat1 = new Cat("大白","Black")
alert(cat1.type) // 输出Animal
复制代码
这个extnd函数也是YUI库实现继承的方法
原文:http://www.ruanyifeng.com/blog/2010/05/object-oriented_javascript_inheritance.html