构造函数绑定
//基类建筑物
var building = function () {
this.spec = "building";
};//address:房子地址,toward:房子朝向
var house = function (address, toward) {
this.address = address;
this.toward = toward;
};//使房子继承建筑物
//使用call或者apply方法,将父对象的构造函数绑定在子对象上,在子对象构造函数中加一行
house = function (address, toward) {
building.apply(this, arguments);
this.address = address;
this.toward = toward;
};var redHouse = new house("四川省成都市高兴西区星辉街9527号","坐北朝南");
alert(redHouse.spec); //building
prototype模式
house.prototype = new building();
house.prototype.constructor = house;
redHouse = new house("四川省成都市高兴西区星辉街9527号", "坐北朝南");
alert(redHouse.species); //building
参考文章:http://www.ruanyifeng.com/blog/2010/05/object-oriented_javascript_inheritance.html
时间: 2024-10-08 20:04:36