`
tinalucky
  • 浏览: 7965 次
  • 性别: Icon_minigender_1
  • 来自: 广州
最近访客 更多访客>>
社区版块
存档分类
最新评论

对象冒充 object masqurading

    博客分类:
  • js
阅读更多
function Car(color){
	this.color = color;
	this.run = function(){document.write("车开了<br/>");};
}

function Bus(color, owner){
	//获取指向Car的指针
	this.newMethod = Car;
	this.newMethod(color);
	//删除指针
	delete this.newMethod;
	
	//定义新方法/属性
	this.owner = function(){document.write("这是属于" + owner + "的车<br/>");};
}

var car = new Car("红色");
var bus = new Bus("黄色", "老板");
car.run();
bus.owner();
document.write("这车的颜色是:" + bus.color + "<br/>");
bus.run();

有趣的是对象冒充可以支持多继承,在函数休内定义多个指向不同类的属性则可
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics