[Javascript] Understanding the .constructor property on JavaScript Objects
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的更多相关文章
- Understanding the Module Pattern in JavaScript
Understanding the Module Pattern in JavaScript Of all the design patterns you are likely to encounte ...
- 【转】JavaScript中的constructor与prototype
最初对js中 object.constructor 的认识: 在学习JS的面向对象过程中,一直对constructor与prototype感到很迷惑,看了一些博客与书籍,觉得自己弄明白了,现在记录如下 ...
- 深入浅析JavaScript中的constructor
constructor 属性返回对创建此对象的数组函数的引用.本文给大家介绍JavaScript中的constructor ,需要的朋友参考下吧 定义和用法 constructor 属性返回对创建此对 ...
- (六)我的JavaScript系列:更好的JavaScript之CoffeeScript
世界上的很多天才都在为构建更好的JavaScript而努力.已经有了很多尝试,其中最有前途的,无非就是CoffeeScript和TypeScript了.面对CoffeeScript,我有一见如故的感觉 ...
- <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 ...
- JavaScript快速入门(四)——JavaScript函数
函数声明 之前说的三种函数声明中(参见JavaScript快速入门(二)——JavaScript变量),使用Function构造函数的声明方法比较少见,我们暂时不提.function func() { ...
- JavaScript快速入门(一)——JavaScript概览
JavaScript是什么? JavaScript的诞生 在1995年前后,当时世界上的主流带宽为28.8Kbps,现在世界平均下载带宽为21.9Mbps(数据来源于http://www.netind ...
- JavaScript 是如何工作的:JavaScript 的共享传递和按值传递
摘要: 原始数据类型和引用数据类型的副本作为参数传递给函数. 原文:JavaScript 是如何工作的:JavaScript 的共享传递和按值传递 作者:前端小智 Fundebug经授权转载,版权归原 ...
- JavaScript 是如何工作的:JavaScript 的内存模型
摘要: 从内存角度理解 let 和 const 的意义. 原文:JavaScript 是如何工作的:JavaScript 的内存模型 作者:前端小智 Fundebug经授权转载,版权归原作者所有. 这 ...
随机推荐
- AndroidStudio连不上天天模拟器
问题:天天模拟器经常无法被Android Studio读取出来: 解决方法:手动连接它的端口: 方法一:找到Android\SDK\platform-tools目录,在当前目录下打开命令行窗口(shi ...
- SFM作业
代码:https://github.com/jianxiongxiao/SFMedu PPT:http://3dvision.princeton.edu/courses/SFMedu/slides.p ...
- java根据freeMark模板生成内容
根据ftl模板生成文件内容可以用来生成代码模板,如下所示: aa.ftl name : ${name} age : ${age} aa.java package mall_tools; import ...
- 配置maven报错 the java_home environment variable is not defined correctly ......
the java_home environment variable is not defined correctly This environment variable is needed to r ...
- Sqlserver添加加字段、删除字段、修改字段类型、修改字段名、修改字段默认值
参考:https://www.cnblogs.com/pangpanghuan/p/6432331.html 初始化表: --.添加字段 --1.1.为null alter table DataTab ...
- luogu P2734 游戏 A Game
https://www.luogu.org/problemnew/show/P2734 数据范围比较小,二位DP可做,而luogu 3004,虽然几乎一模一样(只是数据范围大点),则需要压维. 定义f ...
- 交叉编译OpenCV的教程——基于aarch64-linux-gnu的交叉编译器
1.获取OpenCV3.3.1的源码 地址:https://pan.baidu.com/s/1lnKDThiWg-2QDXNEzVAqrA 提取码:vmn4 2.解压源码包 命令:unzip open ...
- POJ-3278 抓住这头牛
广搜解决. 广搜搜出最短路,直接输出返回就行了. 每个点只搜一次,而且界限进行一次判断. else 语句里面不要用if else if,这样的话就直走一条路了. #include <ios ...
- Linux常用命令大全--有关磁盘空间的命令
1.mount 命令的功能是挂载文件系统,可以挂载硬盘.光盘.软盘,也可以挂载NFS网络文件系统 mount -t 设备类型 存放目录 mount IP地址:/所提供的目录 存放目录 (无) 不加任何 ...
- javax.servlet.jsp.JspTagException: Neither BindingResult nor plain target object for bean (蛋疼死我了)
1为抛出异常原因,2为异常解决方法. 原因: 进入spring:bind标签源码你可以看到 Object target = requestContext.getModelObject(beanNa ...