JS对象继承

来源:互联网 发布:excel怎么标注重复数据 编辑:程序博客网 时间:2024/06/04 21:37

function inheritPrototype(subType,superType){    var prototype = object(superType.prototype);    prototype.constructor = subType;    //将原型指向sub对象    subType.prototype = prototype;}
function object(o){    //创建一个空的构造函数    function F(){};    //空构造函数的原型对象指向o这个对象    F.prototype = o;    return new F();}

//父类

function Father(){    this.name = 'zhang';        this.color = ['red','green'];}Father.prototype.sayName = function({console.log(this.name)}

//子类

function Son(){    //call 借用父类构造函数    Father.call(this);//自动执行改变this指向    this.age=18;    }inheritPrototype(Son,Father);var son = new Son();console.log(son.color);

原创粉丝点击