构造函数继承
2014-12-04 11:47
113 查看
function SuperType(){ this.colors = [1,2,3]; } function SubType(){ //继承属性 SuperType.call(this); } var in1 = new SubType(); in1.colors.push(4); alert(in1.colors); var in2 = new SubType(); in2.colors.push(5); alert(in2.colors);
在子类构造函数中执行超类的函数,则子类的实例中都会有自己的colors属性副本
参数式继承
function SuperType(name){ this.name = name; } function SubType(name){ //继承属性 SuperType.call(this,name); } var in1 = new SubType("Jack"); alert(in1.name); //jack var in2 = new SubType("gogo"); alert(in2.name); //gogo
优点:每个子类实例都有自己的属性副本
缺点:无法复用函数
相关文章推荐
- c++ 继承关系的构造函数
- Javascript面向对象编程:构造函数的继承
- Javascript面向对象编程(二):构造函数的继承
- C++ 基类构造函数带参数的继承方式及派生类的初始化
- C++中继承和调用父类的构造函数方法,外加MFC常见dialog继承
- PHP面向对象 实例化 构造函数 封装 继承 静态
- C++中类的继承与Java中的不同,C++的派生类不能继承父类的构造函数和析构函数(不一定正确)
- ThinkPHP - 进行继承时的 构造函数
- Javascript面向对象编程(三):非构造函数的继承
- Javascript面向对象编程(二):构造函数的继承
- C++继承中构造函数调用顺序
- c++ 构造函数 继承 调用规则
- 构造函数的继承(转自http://www.ruanyifeng.com/blog/2010/05/object-oriented_javascript_inheritance.html)
- 继承 构造函数
- (转)Javascript面向对象编程(三):非构造函数的继承
- 构造函数可以被继承吗?
- Javascript面向对象编程(二):构造函数的继承
- python类继承构造函数的执行问题解析
- Javascript面向对象编程(三):非构造函数的继承
- 继承关系中,静态语句块,非静态语句块,和构造函数执行顺序问题。