JavaScript面向对象编程(11)其他继承方式
2014-12-25 11:40
260 查看
1、将父对象作为新对象的原型
function object(o) {
var n;
function F() {}
F.prototype = o;
n = new F();
n.uber = o;
return n;
}
var shape = {
name: 'Shape',
toString: function(){
if(this.uber)
return this.uber.toString() + ', ' + this.name;
else
return this.name;
}
}
var twoDShape = object(shape);
twoDShape.name = "2 D Shape";
alert(twoDShape.toString());
2、在子构造器中调用父构造器Parent.apply();并传递参数,这样与父对象的this绑定的属性同时成为子对象的属性
function Shape(id) {
this.id = id;
}
Shape.prototype.name = 'shape';
Shape.prototype.toString = function(){return this.name;};
function Triangle() {
Shape.apply(this, arguments);//原生属性继承
}
function object(o) {
var n;
function F() {}
F.prototype = o;
n = new F();
n.uber = o;
return n;
}
var shape = {
name: 'Shape',
toString: function(){
if(this.uber)
return this.uber.toString() + ', ' + this.name;
else
return this.name;
}
}
var twoDShape = object(shape);
twoDShape.name = "2 D Shape";
alert(twoDShape.toString());
2、在子构造器中调用父构造器Parent.apply();并传递参数,这样与父对象的this绑定的属性同时成为子对象的属性
function Shape(id) {
this.id = id;
}
Shape.prototype.name = 'shape';
Shape.prototype.toString = function(){return this.name;};
function Triangle() {
Shape.apply(this, arguments);//原生属性继承
}
相关文章推荐
- 再谈javascript面向对象编程(javascript的两种继承方式)
- JavaScript面向对象编程(继承实现方式)
- javascript继承学习系列之五:其他方式
- Javascript之继承(其他方式)
- 在JavaScript面向对象编程中使用继承(1)
- 在JavaScript面向对象编程中使用继承(4)
- 在JavaScript面向对象编程中使用继承(2)
- 在JavaScript面向对象编程中使用继承(5)
- 在JavaScript面向对象编程中使用继承(2)
- JavaScript 的继承方式与取舍
- 在JavaScript面向对象编程中使用继承(5)
- [转载]JavaScript 面向对象编程, 第一部分: 继承
- JavaScript继承实现方式一览
- 在JavaScript面向对象编程中使用继承(3)
- JavaScript面向对象编程 :继承和重载
- javascript 继承实现方式
- 在JavaScript面向对象编程中使用继承(5)
- 在JavaScript面向对象编程中使用继承(4)
- 在JavaScript面向对象编程中使用继承(2)
- [原创] JavaScript面向对象编程(1) :继承