javascript 点点滴滴 constructor 小点

作为前端三剑客 html--css--javascript  

基础差了果真是 寸步难行啊 。今天看到钗神的一篇文章 讲解 javascript继承。才刚刚开始看就蒙蔽了 好一会:直接看代码吧

(function () {
    var Person = function (name) {
        this.name = name;
    };
    //Person.prototype = {};//这句将影响十分具有constructor属性
    Person.prototype.getName = function () {
        return this.name;
    };

    var Student = function (name, sex, id) {
        this.name = name || '无名氏';
        this.sex = sex || '不明';
        this.id = id || '未填'; //学号
    };
    //相当于将其prototype复制了一次,若是包含constructor的话将指向Person
    Student.prototype = new Person();
    Student.prototype.getId = function () {
        return this.id;
    }
    var y = new Person();
    var s = new Student;
    var s1 = y instanceof Person;
    var s2 = s instanceof Student;
    var s3 = s instanceof Person;
    var s4 = Student.prototype.constructor === Person;
    var s5 = Student.constructor === Person;
    var s6 = Student.constructor === Function;

    var s = '';
})();

  // true

  // true

  // true

  // true

  // false

  // true

 

  这里 为什么第5个 是false 

var s5 = Student.constructor === Person;?  我想了大楷20分钟的样子 期间各种断点调试 。
最后才发现 Student是一个函数 而函数 是一个对象 在javascript里面 大部份内置对象都有自己的constructor属性 。所以函数作为对象也有 ,而作为构造函数的constructor属性 自然指向的是
Function这个 所有函数的构造函数了。哎 基础渣简直汗颜啊。。。呜呜
posted @ 2017-09-10 23:34  蜀-书心  阅读(102)  评论(0编辑  收藏  举报