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. 如何快速上手基础的CSS3动画

    前言 说起CSS3动画,就必须说说 transform,translate,transition,animation这4个属性,transform主要定义元素的动作,比如旋转.倾斜.位移等,trans ...

  2. CPP-STL:vector容器

    1.     vector容器简介: vector向量容器是一种随机访问的数组类型,它提供了对数组元素的快速访问.随机访问,以及在序列尾部快速.随机地插入和删除操作.它类似于数据结构中的队列.数组和堆 ...

  3. win10 配置系统默认utf-8编码

    win10 配置系统默认utf-8编码 系统  win10 配置系统默认utf-8编码 Windows系统默认字符编码为gbk编码,开发项目编码一般为UTF-8,在我们执行程序及进行程序编码过程中编码 ...

  4. java读取nc文件的问题,前端ajax 发送参数进行交互的实例

    1.问题背景: 需要解析nc文件的数据源,获取一个三维数据,并计算器开发值. java 后台处理: 定以一个实例来接收解析的数据并返回给前端. package cn.edu.shou.domain; ...

  5. 【模板】插头dp

    题目描述 题解: 插头$dp$中经典的回路问题. 首先了解一下插头. 一个格子,上下左右四条边对应四个插头.就像这样: 四个插头. 一个完整的哈密顿回路,经过的格子一定用且仅用了两个插头. 所以所有被 ...

  6. Oracle批量更新数据,使用begin end

    /* 使用begin end批量更新 注意end后面必须使用;结束 并且每条update语句都要用;来结束 所以close为;END; 是为了补全语法 */ <foreach collectio ...

  7. 5 SQL 复杂查询

    5 复杂查询 5-1 视图 究竟视图是什么呢?如果用一句话概述的话,就是“从SQL的角度来看视图就是一张表”.实际上,在SQL语句中并不需要区分哪些是表,哪些是视图. 那么视图和表到底右什么不同呢?区 ...

  8. mysql单实例多库与多实例单库

    一.单实例多库: 一个mysql实例,创建多个数据目录. 规划: 实例路径:/usr/local/mysql 数据目录路径: (1)/usr/local/mysql/data (2)/usr/loca ...

  9. 《算法导论》 — Chapter 10 基本数据结构

    序 在本章中,要讨论如何通过使用了指针的简单数据结构表示动态集合.有很多的复杂的数据结构可以用指针来构造,本章介绍几种基本数据结构,包括栈.队列.链表,以及有根树. GitHub 第十章 程序实现代码 ...

  10. ubuntu安装远程桌面连接工具

    1. 安装xrdp sudo apt-get -y install xrdp   2.安装vnc4server sudo apt-get install vnc4server   3.安装xubunt ...