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
Conclusion
  1. Given a new object Obj. Obj does not have its own constructor property. Only Obj.__proto__ has the constructor.
  2. When a function Func is declared, an object Func.prototype is created too. Func.prototype is created with a property constructor:Func.
  3. (Obj1 instanceof Obj2) tracks down the __proto__ link of Obj1 to see if Obj2.prototype exists
  4. 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的更多相关文章

  1. javascript中prototype、constructor以及__proto__之间的三角关系

    三者暧昧关系简单整理 在javascript中,prototype.constructor以及__proto__之间有着“著名”的剪不断理还乱的三角关系,楼主就着自己对它们的浅显认识,来粗略地理理以备 ...

  2. Javascript中的__proto__、prototype、constructor

    今天重温了下Javacript,给大家带来一篇Javascript博文,相信对于Javacript有一定了解的人都听过prototype原型这个概念,今天我们深度的分析下prototype与__pro ...

  3. Javascript Prototype __proto__ constructor 三者的关系

    JavaScript三大毒瘤 --- this,原型链,作用域 在我等菜鸟一步一步升级中的过程中,这三个概念总是困扰这我们(可能只有我吧,我比较蠢).这三个东西往往都很绕,今天我就来分享一下我对原型. ...

  4. JavaScript中的 prototype 和 constructor

    prototype属性  任何js函数都可以用作构造函数, 而构造函数需要用到prototype属性, 因此, 每个js函数F(除了ES5的Function.bind()方法返回的函数外) 都自动拥有 ...

  5. javascript中的prototype和constructor

    构造函数 我们知道,ECMAScript5中的Object.Array.Date.RegExp.Function等引用类型都是基于构造函数的,他们本身就是ECMAScript5原生的构造函数.比如,我 ...

  6. js中prototype,constructor的理解

    连看4篇前辈的文章,记录一些知识点 Javascript继承机制的设计思想 Javascript 面向对象编程(一):封装 Javascript面向对象编程(二):构造函数的继承 Javascript ...

  7. 关于JS call apply 对象、对象实例、prototype、Constructor、__proto__

    关于call与apply的理解容易让人凌乱,这里有个方法可供参考 tiger.call(fox,arg1,arg2...) tiger.apply(fox,[arg1,arg2...]) 理解为 fo ...

  8. JS中关于构造函数、原型链、prototype、constructor、instanceof、__proto__属性

    在Javascript不存在类(Class)的概念,javascript中不是基于类的,而是通过构造函数(constructor)和原型链(prototype chains)实现的.但是在ES6中引入 ...

  9. js in depth: arrow function & prototype & this & constructor

    js in depth: arrow function & prototype & this & constructor https://developer.mozilla.o ...

随机推荐

  1. AndroidService 深度分析(2)

    AndroidService 深度分析(2) 上一篇文章我们Service的生命周期进行了測试及总结. 这篇文章我们介绍下绑定执行的Service的实现. 绑定执行的Service可能是仅为本应用提供 ...

  2. OCP解决问题053-16 MEMORY_TARGET

    16.Setting which of the following initialization parameters enables Automatic Memory Management? A. ...

  3. 在深入分析:Android在app之间的相互作用(一个,使用Action)

    我们开发Android App时间应用,有些需求,我们需要启动另一App为了应对一些逻辑.例如,我们需要映射基于地址调用系统或相关Map App,所以,我们不自己有App在相应的功能的制备.而是通过I ...

  4. UI —— 计算器

    #import <UIKit/UIKit.h> @interface MyViewController :UIViewController { NSInteger _firstName; ...

  5. 【Nginx】磁盘文件写入飞地发

    文章继续.什么时候Nginx当用户请求一个文件,这将无法读取该文件的内容加载到内存,然后从内存发送,但电话sendfile况下,从内核直接发送出去.这样做显然效率要更高.Nginx也为我们封装好了一系 ...

  6. 【C语言探索之旅】 第一部分第十课:练习题+习作

    内容简介 1.课程大纲 2.第一部分第十课: 练习题+习作 3.第二部分第一课预告: 模块化编程 课程大纲 我们的课程分为四大部分,每一个部分结束后都会有练习题,并会公布答案.还会带大家用C语言编写三 ...

  7. Android ListView分页载入(服务端+android端)Demo

    Android ListView分页载入功能 在实际开发中经经常使用到,是每一个开发人员必须掌握的内容,本Demo给出了服务端+Android端的两者的代码,并成功通过了測试. 服务端使用MyEcli ...

  8. js比量undefined种类

    js比量undefined种类 if (reValue== undefined) {     alert("undefined"); } 发现推断不出来.最后查了下资料要用type ...

  9. 房费制 它 结账BUG

    声明:以下内容仅仅是对在桌子上的卡与卡表的后面,适合学生的表!     最近,我们已经开始做VB.NET系统重构版,在这里跟大家聊聊我在机房收费系统中发现的漏洞. 在机房收费系统中有这样一个窗口--结 ...

  10. poj 2001 Shortest Prefixes(特里)

    主题链接:http://poj.org/problem?id=2001 Description A prefix of a string is a substring starting at the ...