2019-06-18 今日头条面试题。 合理设计这两个类。 2。 student 继承person 3 不能使用class

//1。 合理设计这两个类。 2。 student 继承person  3 不能使用class


function Person(id, name) {
    if(!(this instanceof Person)){
        return new Person(id,name);
    }
    this.id = id;
    this.name = name;
}


Person.prototype.say = function (text) {
    console.log("say:" + text)
};


function Student(id, name) {
    if(!(this instanceof Student)){
        return new Student(id,name);
    }
    Person.call(this, id, name)
}

Student.prototype = new Person();
Student.prototype.learn = function () {
    console.log('learn')
};




var s = new Student(111, 'tom');
var s2 = Student(111, 'tom');   // 支持不使用new关键字创建对象。


s.say("hello");
console.log(s instanceof Person);
console.log(s instanceof Student);

  

 

posted on 2019-06-18 19:10  袜子破了  阅读(170)  评论(1编辑  收藏  举报