Prototype and Constructor in JavaScript

时间:2023-01-20 09:17:41

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. centos7网络设置

    1.设置虚拟机网络连接方式 2.启动改为从驱动启动 3.启动虚拟机,使用命令 ip addr 查看ip,发现网卡配置为 eno16777736 如果找不到网卡配置文件,请返回安装过程,查看是否正确选择 ...

  2. ThreadLocal类的理解

    首先,ThreadLocal 不是用来解决共享对象的多线程访问问题的,一般情况下,通过ThreadLocal.set() 到线程中的对象是该线程自己使用的对象,其他线程是不需要访问的,也访问不到的.各 ...

  3. iOS设备、Icon、LaunchImage、图片分辨率

    iOS设备 iOS设备的屏幕的大小.分辨率以及比例因数(Scale Factor)[1]. iPhone 设备 宽(inch) 高(inch) 对角线(inch) 逻辑分辨率(point) Scale ...

  4. 使用Raphael 画图(四) 路径(一) (javascript)

    这章介绍路径,本人觉得这是比较难和精髓的一部分. 先介绍基本知识: 可以参考: http://www.chinasvg.com/support/svg-tutorial/svg-path-direct ...

  5. BZOJ-1225-[HNOI2001] 求正整数

    Description 对于任意输入的正整数n,请编程求出具有n个不同因子的最小正整数m.例如:n=4,则m=6,因为6有4个不同整数因子1,2,3,6:而且是最小的有4个因子的整数. Input n ...

  6. Android ClassLoader详解

    我们知道不管是插件化还是组件化,都是基于系统的ClassLoader来设计的.只不过Android平台上虚拟机运行的是Dex字节码,一种对class文件优化的产物,传统Class文件是一个Java源码 ...

  7. 后台管理系统好用的UI框架

    https://www.layui.com/demo/form.html

  8. Cs231n课堂内容记录-Lecture2-Part1 图像分类

    Lecture 2 课程内容记录:(上)https://zhuanlan.zhihu.com/p/20894041?refer=intelligentunit (下)https://zhuanlan. ...

  9. 使用Sublime Text 2 和 MinGW 搭建C开发环境

    使用工具 Sublime Text 2(Download) MinGW(Download)或者使用CygWin(Download)亦可 1.配置环境变量 下载和安装Sublime Text 2和Min ...

  10. Python读取PE文件(exe/dll)中的时间戳

    代码原文地址: https://www.snip2code.com/Snippet/144008/Read-the-PE-Timestamp-from-a-Windows-Exe https://gi ...