Constructor functions hold an interesting purpose in JavaScript. Unlike in classical languages, they do not always mean created by. In this lesson we’ll use the new keyword to make a constructor call and work with the .constructor property.

When we define a function:

function Foo(){
//.
}

It has one prototype prop defined which is constructor. And it points to the Foo function itself.

console.log(Foo.prototype.constructor === Foo) // true

And if we have an instance created by new keyword:

const f= new Foo();

Then:

console.log(f.constructor === Foo) // true

So which mean:

f.constructor === Foo.prototype.constructor

This prototype chain can be broken when we reassgin the Foo.prototype = {}:

Foo.prototype = {}; // now we reassign to an empty object.
console.log(Foo.prototype.constructor === Foo); // false
console.log(f.constructor === Foo); //true
console.log(f.constructor === Foo.prototype.constructor); // false

We can use Object.defineProperty to reassign the constructor to the Foo.prototype:

Foo.prototype = {};
Object.defineProperty(Foo.prototype, "constructor", {
enumerable: false,
writable: true,
configurable: true,
value: Foo
}); console.log(Foo.prototype.constructor === Foo); // true
console.log(f.constructor === Foo); // true
console.log(f.constructor === Foo.prototype.constructor); // true

[Javascript] Understanding the .constructor property on JavaScript Objects的更多相关文章

  1. Understanding the Module Pattern in JavaScript

    Understanding the Module Pattern in JavaScript Of all the design patterns you are likely to encounte ...

  2. 【转】JavaScript中的constructor与prototype

    最初对js中 object.constructor 的认识: 在学习JS的面向对象过程中,一直对constructor与prototype感到很迷惑,看了一些博客与书籍,觉得自己弄明白了,现在记录如下 ...

  3. 深入浅析JavaScript中的constructor

    constructor 属性返回对创建此对象的数组函数的引用.本文给大家介绍JavaScript中的constructor ,需要的朋友参考下吧 定义和用法 constructor 属性返回对创建此对 ...

  4. (六)我的JavaScript系列:更好的JavaScript之CoffeeScript

    世界上的很多天才都在为构建更好的JavaScript而努力.已经有了很多尝试,其中最有前途的,无非就是CoffeeScript和TypeScript了.面对CoffeeScript,我有一见如故的感觉 ...

  5. <a href="javascript:void(0);" id='test' onclick="javascript:alert('即将上线,敬请期待!');"><em class="rmwd"></em>征稿平台</a>

    <a href="javascript:void(0);" id='test' onclick="javascript:alert('即将上线,敬请期待!');&q ...

  6. JavaScript快速入门(四)——JavaScript函数

    函数声明 之前说的三种函数声明中(参见JavaScript快速入门(二)——JavaScript变量),使用Function构造函数的声明方法比较少见,我们暂时不提.function func() { ...

  7. JavaScript快速入门(一)——JavaScript概览

    JavaScript是什么? JavaScript的诞生 在1995年前后,当时世界上的主流带宽为28.8Kbps,现在世界平均下载带宽为21.9Mbps(数据来源于http://www.netind ...

  8. JavaScript 是如何工作的:JavaScript 的共享传递和按值传递

    摘要: 原始数据类型和引用数据类型的副本作为参数传递给函数. 原文:JavaScript 是如何工作的:JavaScript 的共享传递和按值传递 作者:前端小智 Fundebug经授权转载,版权归原 ...

  9. JavaScript 是如何工作的:JavaScript 的内存模型

    摘要: 从内存角度理解 let 和 const 的意义. 原文:JavaScript 是如何工作的:JavaScript 的内存模型 作者:前端小智 Fundebug经授权转载,版权归原作者所有. 这 ...

随机推荐

  1. 一条update语句优化小记

    遇到性能问题的sql如下: sql1: UPDATE amlclientlevel a SET    a.client_value = (SELECT l.client_value           ...

  2. 如何给run()方法传参数

    实现的方式主要有三种 1.构造函数传参 2.成员变量传参 3.回调函数传参 问题:如何实现处理线程的返回值? 1.主线程等待法(优点:实现起来简单,缺点:需要等待的变量一多的话,代码就变的非常臃肿.而 ...

  3. dubbo负载均衡策略和集群容错策略

    dubbo负载均衡策略 random loadbalance 默认情况下,dubbo是random load balance随机调用实现负载均衡,可以对provider不同实例设置不同的权重,会按照权 ...

  4. 访问修饰词--Java

    public(公共的) 权限: 完全公开 protected(受保护的) 权限: 对子类和同包中的其他类公开 default(默认的,可不写) 权限: 对同包中的其他类公开 private(私有的) ...

  5. MyBatis的动态sql小练习,小回顾

    关键字if+trim trim可以去除多余的关键字,是where和set的组合 trim标记是一个格式化的标记,可以完成set或者是where标记的功能,如下代码: <trim prefix=& ...

  6. 怎么给xshell服务配置密钥远程登陆

    xshell密钥登陆 1.找一台测试服务器xiuxiu-test生成密钥对2. cd ./.ssh/ && 把公钥放在~/.ssh/authorized_keys文件中  cat id ...

  7. 07 mongodb

    mongodb mongodb简介 简介 MongoDB是一个基于分布式文件存储的数据库.由C++语言编写.旨在为Web应用提供可扩展的高性能数据存储解决方案.    MongoDB是一个介于关系数据 ...

  8. reactNative 介绍

    React Native (简称RN)是Facebook于2015年4月开源的跨平台移动应用开发框架,是Facebook早先开源的UI框架 React 在原生移动应用平台的衍生产物,目前支持iOS和安 ...

  9. Relocation(状压DP)

    Description Emma and Eric are moving to their new house they bought after returning from their honey ...

  10. 在mac下安装matplotlib,xlrd

    brew install freetype brew install libpng sudo easy_install pip#图形显示模块 sudo pip install matplotlib 输 ...