首页 > 其他 > 详细

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

时间:2019-06-18 20:28:27      阅读:115      评论:0      收藏:0      [点我收藏+]
//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);

  

 

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

原文:https://www.cnblogs.com/lhp2012/p/11047076.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!