在学习过程中对js的constructor的作用产生了疑问。下面是学习的资料进行梳理

function Person(area){
this.type = 'person';
this.area = area;
}
Person.prototype.sayArea = function(){
console.log(this.area);
}
var Father = function(age){
this.age = age;
}
Father.prototype = new Person('Beijin');
console.log(Person.prototype.constructor) //function person()
console.log(Father.prototype.constructor); //function person()
Father.prototype.constructor = Father; //修正
console.log(Father.prototype.constructor); //function father()
var one = new Father();
Father.prototype.constructor = Father,这里修正了的Father的constructor。我们知道prototype下的constructor属性返回对创建此对象的函数的引用。
  
一、不修正时
  Father.constructor = function Function(),Father.prototype.constructor = function Person(),这里引出
一个题外话,为什么Father.constructor !== Father.prototype.constructor。 1. _proto_是所有对象(包括函数)都有的,它才叫做对象的原型,原型链就是靠它形成的。
2. prototype只有函数(准确地说是构造函数)才有的。它跟原型链没有关系。它的作用是:构造函数new对象的时候,告诉构造函数新创建的对象的原型是谁。
  Father.constructor,是从Father的原型链查找属性,也就是__proto__,因为Father继承的是Function(){},而Function(){}的constructor就是它自己
所以Father.constructor = function Function();
  为什么Father.prototype.constructor 是 function Person(),首先Father.prototype = new Person('Beijin');当我们用new
运算符会产生以下步骤:

  1. var obj={}; 也就是说,初始化一个对象obj。
  2. obj.__proto__=a.prototype;
  3. a.call(obj);也就是说构造obj,也可以称之为初始化obj。
  也就是说(new Person('Beijin')).__proto__ === Person.prototype //true 
前面我们说过new Person('Beijin')对象是没有prototype的,prototype只有函数才有;Father.prototype.constructor将会沿着new Person('Beijin')的
原型链向下查找constructor,new Person('Beijin')没有constructor就去它的__proto__找,因为(new Person('Beijin')).__proto__ === Person.prototype
而Person.prototype.constructor == function Person(),所以 Father.prototype.constructor == Person.prototype.constructor //function Person()
  当我们var one = new Father(25) 时 ,one.constructor = Father.prototype.constructor,所以one.constructor指向function Person(),
二、修正时
当我们加上Father.prototype.constructor = Father;对象one的原型链变成

显而易见,one.constructor = Father.prototype.constructor = function Father(); 三、作用
var man;
(function(){
function Father (name) {
this.name = name;
} Father.prototype.sayName= function () {
console.log(this.name);
}
man = new Father('aoyo');
})()
man.sayName();//aoyo console.log(Father); //Father is not defined

因为Father在闭包中,当我们想对Father类增加方法时可以通过

man.constructor.prototype.sayAge = function(age){
console.log(age);
}
man.sayAge('20'); //

如果不进行修正,我们的方法将会添加到Person类,而不是Father类。

												

js中constructor的作用的更多相关文章

  1. js中getBoundingClientRect的作用及兼容方案

    js中getBoundingClientRect的作用及兼容方案 1.getBoundingClientRect的作用 getBoundingClientRect用于获取某个html元素相对于视窗的位 ...

  2. JS中冒号的作用

    JS中冒号的作用1.声明对象的成员2.switch语句分支3.三元表达式 1.声明对象的成员 var Book = { Name: '法', Price: 100, Discount : functi ...

  3. js中with的作用

    js中with的作用当一个对象有多个需要操作的属性或方法时,可以使用如<体>试验<script type=“text/javascript”>var o=文件.创建元素(“DI ...

  4. js中constructor和prototype

    在最开始学习js的时候,我们在讲到原型链和构造函数的时候经常会有一个例子 如果我们定义函数如下: function Foo() { /* .. */ } Foo.prototype.bar = fun ...

  5. JS中constructor与prototype关系概论

    在学习JS的面向对象过程中,一直对constructor与prototype感到很迷惑,看了一些博客与书籍,觉得自己弄明白了,现在记录如下:     我们都知道,在JS中有一个function的东西. ...

  6. js中return的作用及用法

    这里面的return含有一些细节知识: 例如:onClick='return add_onclick()'与 onClick='add_onclick()'的区别 JAVASCRIPT在事件中调用函数 ...

  7. vue.js中created方法作用

    这是它的一个生命周期钩子函数,就是一个vue实例被生成后调用这个函数.一个vue实例被生成后还要绑定到某个html元素上,之后还要进行编译,然后再插入到document中.每一个阶段都会有一个钩子函数 ...

  8. JS 、JQ 获取宽高总结 & JS中getBoundingClientRect的作用及兼容方案

    1.getBoundingClientRect的作用 getBoundingClientRect用于获取某个html元素相对于视窗的位置集合.   执行 object.getBoundingClien ...

  9. JS中constructor,prototype

    First: this this定义: this就是函数赖以执行的对象. 分析这句话: 1. this是对象. 2. this依赖函数执行的上下文环境. 3. this存在函数中. 直接看例子: al ...

随机推荐

  1. windows上putty访问ubuntu

    1. Ubuntu中安装ssh-server实现远程登录 a) 安装:sudo apt-get install openssh-server b) 开启服务:sudo /etc/init.d/ssh ...

  2. cf C. Knight Tournament

    http://codeforces.com/contest/357/problem/C #include <cstdio> #include <cstring> #includ ...

  3. 面试题 43 n 个骰子的点数

    ; void printfProbability(int number) { ) return; ]; p[] = ]; p[] = ]; memset(p[], , )); memset(p[], ...

  4. cf492A Vanya and Cubes

    A. Vanya and Cubes time limit per test 1 second memory limit per test 256 megabytes input standard i ...

  5. WEB 移动网站 手机点击 打电话 发短信

    原文地址: http://www.blesswe.com/portal.php?mod=view&aid=428 我们在手机浏览网页是希望用户看到手机号码点击就可以直接打电话或发短信,下面我们 ...

  6. openstack组件手动部署整合

    preface:当你完全且正确的配置好整个OpenStack ENV 你将能看到的和体验到的!!! 我们先来看看简单效果吧,祝君能在这条路上走的更远,更好;

  7. mysql插入数据时,中文乱码

    MySQL 插入数据时,中文乱码问题的解决(转) 当向 MySQL 数据库插入一条带有中文的数据形如 insert into employee values(null,'张三','female','1 ...

  8. QTableWidget查找指定项(由github处学习到)

    from PyQt4 import QtGui, QtCore class Window(QtGui.QWidget): def __init__(self, rows, columns): QtGu ...

  9. python批量下载

    # -*- coding: utf-8 -*-__author__ = 'Administrator'from PyQt4.Qt import *from PyQt4.QtCore import *f ...

  10. HDU 3622 Bomb Game(2-sat)

    HDU 3622 Bomb Game 题目链接 题意:求一个最大半径,使得每一个二元组的点任选一个,能够得到全部圆两两不相交 思路:显然的二分半径,然后2-sat去判定就可以 代码: #include ...