码迷,mamicode.com
首页 > Web开发 > 详细

js面向对象编程(二) 构造函数继承

时间:2015-07-09 12:37:48      阅读:145      评论:0      收藏:0      [点我收藏+]

标签:


 构造函数绑定


//基类建筑物
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







js面向对象编程(一) 封装

参考文章:http://www.ruanyifeng.com/blog/2010/05/object-oriented_javascript_inheritance.html

js面向对象编程(二) 构造函数继承

标签:

原文地址:http://www.cnblogs.com/yuantao333/p/4632523.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!