Prototype and Constructor in JavaScript
The concept of prototype in JavaScript is very confusing, especially to those who come with a C++/JAVA/... background. As an OOP langauge, JavaScript is very different from the languages with
class. Its classless feature make it somehow difficult to understand. To muddy the water, JavaScript uses
prototype-based inheritance but the prototype property of an object is not really its prototype.
As a beginner, I read many articles and do some experiments to tidy up the mess of prototype. Here I would like to share what I have learnt with you. Most of the findings I get is experiment based so you can evaluate them by
typing simple statements in the editor you use!
As Linus said, "Talk is cheap. Show me the code." Let me show you the demonstration code below. To begin, we have:
function abc()
{
// nothing
}
var x = new abc();
First, let's look at the variable x: (all boolean expression evaluate to true in this article)
x.__proto__ === abc.prototype; // abc.prototype is used to build x
x.constructor === abc; // not really, see (1)
typeof x.prototype === "undefined"; // cannot be used to build new object
Then, we look at the function abc:
typeof abc === "function";
abc.__proto__ === Function.prototype;
abc.constructor === Function; // not really, see (1)
typeof abc.prototype === "object";
(1) The x.constructor actually does not exist. x.__proto__.constructor is accessed instead.
abc.prototype.constructor === abc;
x.hasOwnProperty('constructor') === false;
abc.prototype.hasOwnProperty('constructor') === true;
(2) More on abc.prototype.constructor:
abc.prototype = {}; // abc.prototype becomes a new empty object
x instanceof abc === false; // x is no longer an instance of abc, see (3)
x.constructor === abc; // constructor property is still abc, why?
x.__proto__ !== abc.prototype; // because __proto__ points at the old abc.prototype
x = new abc(); // reassign new abc() to x
x instanceof abc === true; // x is an instance of abc again, see (3)
x.__proto__ === abc.prototype; // same now
abc.prototype.hasOwnProperty('constructor'); // the empty object does not has its own constructor
abc.prototype.__proto__.constructor === Object // the constructor of Object.prototype is used
x.constructor === Object; // x.__proto__.__proto__.constructor is accessed
(3) The reason why x is an instance of
Object : tracking down the __proto__ chain
abc.prototype.__proto__ === Object.prototype;
abc.prototype instanceof Object === true; x.__proto__ === abc.prototype;
x instanceof abc === true; x.__proto__.__proto__ === Object.prototype;
x instanceof Object === true;
(4) When properties are added to the __proto__ chain, x access the closest one in chain.
abc.prototype.color = "red";
x.color === "red"; // abc.prototype.color is accessed Object.prototype.food = "potato";
x.food === "potato"; // Object.prototype.food is accessed abc.prototype.food = "apple"; // now abc.prototype also has food property
x.food === "apple"; // food property in abc.prototype is accessed instead
Object.prototype.food === "potato"; // food property in Object.prototype remains unchanged
- Given a new object Obj. Obj does not have its own constructor property. Only Obj.__proto__ has the constructor.
- When a function Func is declared, an object Func.prototype is created too. Func.prototype is created with a property constructor:Func.
- (Obj1 instanceof Obj2) tracks down the __proto__ link of Obj1 to see if Obj2.prototype exists
- When Obj.prop1 is accessed, the whole __proto__ link is tracked to find the property in Obj's closest relative
Do you find it interesting? If yes, please tell me so that I will write more on this topic in the future!
版权声明:本文博主原创文章,博客,未经同意不得转载。
Prototype and Constructor in JavaScript的更多相关文章
- javascript中prototype、constructor以及__proto__之间的三角关系
三者暧昧关系简单整理 在javascript中,prototype.constructor以及__proto__之间有着“著名”的剪不断理还乱的三角关系,楼主就着自己对它们的浅显认识,来粗略地理理以备 ...
- Javascript中的__proto__、prototype、constructor
今天重温了下Javacript,给大家带来一篇Javascript博文,相信对于Javacript有一定了解的人都听过prototype原型这个概念,今天我们深度的分析下prototype与__pro ...
- Javascript Prototype __proto__ constructor 三者的关系
JavaScript三大毒瘤 --- this,原型链,作用域 在我等菜鸟一步一步升级中的过程中,这三个概念总是困扰这我们(可能只有我吧,我比较蠢).这三个东西往往都很绕,今天我就来分享一下我对原型. ...
- JavaScript中的 prototype 和 constructor
prototype属性 任何js函数都可以用作构造函数, 而构造函数需要用到prototype属性, 因此, 每个js函数F(除了ES5的Function.bind()方法返回的函数外) 都自动拥有 ...
- javascript中的prototype和constructor
构造函数 我们知道,ECMAScript5中的Object.Array.Date.RegExp.Function等引用类型都是基于构造函数的,他们本身就是ECMAScript5原生的构造函数.比如,我 ...
- js中prototype,constructor的理解
连看4篇前辈的文章,记录一些知识点 Javascript继承机制的设计思想 Javascript 面向对象编程(一):封装 Javascript面向对象编程(二):构造函数的继承 Javascript ...
- 关于JS call apply 对象、对象实例、prototype、Constructor、__proto__
关于call与apply的理解容易让人凌乱,这里有个方法可供参考 tiger.call(fox,arg1,arg2...) tiger.apply(fox,[arg1,arg2...]) 理解为 fo ...
- JS中关于构造函数、原型链、prototype、constructor、instanceof、__proto__属性
在Javascript不存在类(Class)的概念,javascript中不是基于类的,而是通过构造函数(constructor)和原型链(prototype chains)实现的.但是在ES6中引入 ...
- js in depth: arrow function & prototype & this & constructor
js in depth: arrow function & prototype & this & constructor https://developer.mozilla.o ...
随机推荐
- TCP拥塞控制 (1)
Basic: TCP,传输控制协定,它是目前最广泛使用的网络传输协议.SMTP.SSH.FTP.HTTP等因特网底层协议均是TCP. TCP面向连接,提供端到端的数据可靠传输.连接时三次握手.断开是四 ...
- C3P0具体的配置说明(com.mchange.v2.c3p0.ComboPooledDataSource)
C3P0它是一个开源JDBC连接池,它lib文件夹和Hibernate一起公布,包含了实现jdbc3和jdbc2扩展规范说明的Connection 和Statement 池的DataSources 对 ...
- Java服务器下载速度的限制
没有取之不尽,用之不竭的资源.server有限的带宽.运营商可以限制一点点.近期使用云存储openstack swift待办事项文件存储下载.如果第一个限速code: private Long wri ...
- (ArcGIS API For Silverlight )QueryTask 跨层查询,和监控完整的查询!
(ArcGIS API For Silverlight )QueryTask 跨层查询,和监控完整的查询! 直接在源代码: 定义全局变量: int index=0; /// & ...
- 开源Math.NET基础数学类库使用(06)数值分析之线性方程组直接求解
原文:[原创]开源Math.NET基础数学类库使用(06)数值分析之线性方程组直接求解 开源Math.NET基础数学类库使用系列文章总目录: 1.开源.NET基础数学计算组件Math.NET(一) ...
- 最小二乘法 (转)good
最小二乘法也称为最小平方法,是一种数据优化技术,它通过最小化误差的平方和寻找数据的最佳函数匹配. 最小二乘法最初由高尔顿在创立回归分析的时候提出,现在已经成为探索变量间关系最重要的方法,最小二乘法根据 ...
- 系统如何端子app弄root才干
最近由于调试USB OTG怪东西.这导致USB端口被占用,这项.虽然我是project版本号,但不能运行adb shell,这是不可能的debug该. 所以,我现在是一个系统终端apk,规划 http ...
- 从XML文件乱码问题,探寻其背后的原理(转)
由于网友反应本文图片不能显示,由于时间关系未能及时修正.请访问原文地址: 本文出自http://blog.csdn.net/dinglang_2009/article/details/6895355, ...
- 使用JMX实现的内存监控(转)
public final class MemoryWarningSystem { private static MemoryWarningSystem m_instance = null; /** * ...
- UVa 10397 Connect the Campus
最小生成树 Kruskal #include<cmath> #include<iostream> #include<cstdio> #include<algo ...