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);
相關文章
相關標籤/搜索