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. 雷林鹏分享:Lua 数据类型

    Lua是动态类型语言,变量不要类型定义,只需要为变量赋值. 值可以存储在变量中,作为参数传递或结果返回. Lua中有8个基本类型分别为:nil.boolean.number.string.userda ...

  2. 初探node.js

    一.定义及优势 定义:Node.js是一个基于 Chrome V8 引擎 的 JavaScript 运行时,它以事件驱动为基础实现了非阻塞模型. 优势:由于Web场景下的大多数任务(静态资源读取.数据 ...

  3. 使用阿里云RDS

    1)购买 注意内网免费 外网收费 内网需要跟服务器ECS在同一VPC下 即ECS买在华东1 RDS也必须在华东1 2)使用 配置白名单  全部通过设置为0.0.0.0/0 (不建议) 创建账户 创建数 ...

  4. 《BUG创造队》作业8:软件测试与Alpha冲刺(第四天)

    项目 内容 这个作业属于哪个课程 2016级软件工程 这个作业的要求在哪里 实验十二 团队作业8:软件测试与ALPHA冲刺 团队名称 BUG创造队 作业学习目标 (1)掌握软件测试基础技术.(2)学习 ...

  5. UVa-340-猜数字

    #include <stdio.h> char ans[1000],gus[1000]; int num[10]; int main() { int n,cnt=1; while (sca ...

  6. c++类的单目和双目运算符的重定义

    这个里面需要注意的是对于双目运算符,像是加号,如果是复数加整数是一种情况,而整数加复数又是另一种情况,所以需要重定义两次. 而对于单目运算符,如果是前缀的,直接重定义就可以了,但是如果是后缀的,我们在 ...

  7. 文件默认权限umask掩码

    umask命令 作用:用于显示.设置文件的缺省权限 格式:umask   [-S] -S表示以rwx形式显示新建文件缺省权限 系统的默认掩码是0022 文件创建时的默认权限 = 0666 - umas ...

  8. 聊聊svg

    来源:SVG的用法 补充 CANVAS产生的dom数量比SVG要少 SVG可以使用css设置动画样式 对于动画性能来说,不能说svg或canvas谁更优,而是要看情况: SVG 是一种使用 XML 描 ...

  9. 宝塔apache配置

    apache配置 <VirtualHost *:80> ServerAdmin webmaster@example.com DocumentRoot "/www/wwwroot/ ...

  10. PROPAGATION_REQUIRED,ISOLATION_DEFAULT; '',-java.lang.Exception

    转https://stackoverflow.com/questions/29117679/spring-transactional-management-propagation-required-i ...