码迷,mamicode.com
首页 > 编程语言 > 详细

javascript -->constructor 小点

时间:2017-09-11 00:45:07      阅读:209      评论:0      收藏:0      [点我收藏+]

标签:struct   asc   turn   复制   开始   fun   前端   cti   一个   

作为前端三剑客 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这个 所有函数的构造函数了。哎 基础渣简直汗颜啊。。。呜呜

javascript -->constructor 小点

标签:struct   asc   turn   复制   开始   fun   前端   cti   一个   

原文地址:http://www.cnblogs.com/hfdj/p/7502911.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!