A Plain English Guide to JavaScript Prototypes
When I first started learning about JavaScript object model my reaction was of horror and disbelief. I was totally puzzled by its prototype nature as it was my first encounter with a prototype based language. I didn’t help that JavaScript has a unique take on prototypes as it adds the concept of function constructors. I bet that many of you have had similar experience.
But as I used JavaScript more I didn’t just learn to understand its object model but also started love parts of it. Thanks to JavaScript I have find out the elegance and flexibility of prototypes languages. I am now quite fond of prototype languages because they have a simpler and more flexible object model than class based languages.
Prototypes in Javascript
Most guides / tutorials start explaining JavaScript objects by going directly to ‘constructor functions’, I think this is a mistake, as they introduce a fairly complex concept early on making Javascript look difficult and confusing from the start. Let’s leave this for later. First let’s start with the basics of prototypes.
Prototype chains (aka prototype inheritance)
Every object in Javascript has a prototype. When a messages reaches an object, JavaScript will attempt to find a property in that object first, if it cannot find it then the message will be sent to the object’s prototype and so on. This works just like single parent inheritance in a class based language.
Prototype inheritance chains can go as long as you want. But in general it is not a good idea to make long chains as your code can get difficult to understand and maintain.
The __proto__ object
To understand prototype chains in JavaScript there is nothing as simple as the __proto__property. Unfortunately __proto__ is not part of the standard interface of JavaScript, not at least until ES6. So you shouldn’t use it in production code. But anyway it makes explaining prototypes easy.
// let's create an alien object
var alien = {
kind: 'alien'
} // and a person object
var person = {
kind: 'person'
} // and an object called 'zack'
var zack = {}; // assign alien as the prototype of zack
zack.__proto__ = alien // zack is now linked to alien
// it 'inherits' the properties of alien
console.log(zack.kind); //=> ‘alien’ // assign person as the prototype of zack
zack.__proto__ = person // and now zack is linked to person
console.log(zack.kind); //=> ‘person’
As you can see the __proto__ property is very straightforward to understand and use. Even if we shouldn’t use __proto__ in production code, I think that these examples give the best foundation to understand the JavaScript object model.
You can check that one object is the prototype of another by doing:
console.log(alien.isPrototypeOf(zack))
//=> true
Prototype lookups are dynamic
You can add properties to the prototype of an object at any time, the prototype chain lookup will find the new property as expected.
var person = {} var zack = {}
zack.__proto__ = person // zack doesn't respond to kind at this point
console.log(zack.kind); //=> undefined // let's add kind to person
person.kind = 'person' // now zack responds to kind
// because it finds 'kind' in person
console.log(zack.kind); //=> 'person'
New / updated properties are assigned to the object, not to the prototype
What happens if you update a property that already exists in the prototype? Let’s see:
var person = {
kind: 'person'
} var zack = {}
zack.__proto__ = person zack.kind = 'zack' console.log(zack.kind); //=> 'zack'
// zack now has a 'kind' property console.log(person.kind); //=> 'person'
// person has not being modified
Note that the property ‘kind’ now exists in both person and zack.
Object.create
As explained before __proto__ is not a well supported way of assigning prototypes to objects. So the next simplest way is using Object.create(). This is available in ES5, but old browsers / engines can be shimmed using this es5-shim.
var person = {
kind: 'person'
} // creates a new object which prototype is person
var zack = Object.create(person); console.log(zack.kind); // => ‘person’
You can pass an object to Object.create to add specific properties for the new object
var zack = Object.create(person, {age: {value: 13} });
console.log(zack.age); // => ‘13’
Yes, the object you need to pass is a bit convoluted, but that is the way it is. See the docs here.
Object.getPrototype
You can get the prototype of an object using Object.getPrototypeOf
var zack = Object.create(person);
Object.getPrototypeOf(zack); //=> person
There is no such thing as Object.setPrototype.
Constructor Functions
Constructor functions are the most used way in JavaScript to construct prototype chains. The popularity of constructor functions comes from the fact that this was the only original way for constructing types. It is also an important consideration the fact that many engines are highly optimized for constructor functions.
Unfortunately they can get confusing, they are in my opinion one of the main reasons why new comers find JavaScript puzzling, but they are a big part of the language and we need to understand them well.
Functions as constructors
In JavaScript you create an instance of a function like this:
function Foo(){} var foo = new Foo(); //foo is now an instance of Foo
console.log(foo instanceof Foo ) //=> true
In essence functions when used with the keyword new behave like factories, meaning that they create new objects. The new object they create is linked to the function by its prototype, more on this later. So in JavaScript we call this an instance of the function.
‘this’ is assigned implicitly
When we use ‘new’, JavaScript injects an implicit reference to the new object being created in the form of the ‘this’ keyword. It also returns this reference implicitly at the end of the function.
When we do this:
function Foo() {
this.kind = ‘foo’
} var foo = new Foo();
foo.kind //=> ‘foo’
Behind the scenes it is like doing something like this:
function Foo() {
var this = {}; // this is not valid, just for illustration
this.__proto__ = Foo.prototype; this.kind = ‘foo’ return this;
}
But keep in mind that the implicit ‘this’ is only assigned to a new object when using ‘new’. If you forget ‘new’ keyword then ‘this’ will be the global object. Of course forgetting new is a cause of multiple bugs, so don’t forget new.
One convention that I like is capitalizing the first letter of a function when it is intented to be used as a function constructor, so you now straightaway to you are missing the new keyword.
The ‘function prototype’
Every function in JavaScript has a special property called ‘prototype’.
function Foo(){
} Foo.prototype
As confusing as it may sound, this ‘prototype’ property is not the real prototype (__proto__) of the function.
Foo.__proto__ === Foo.prototype //=> false
This of course generates a lot of confusion as people use the term ‘prototype’ to refer to different things. I think that a good clarification is to always refer to the special ‘prototype’ property of functions as ‘the function prototype’, never just ‘prototype’.
The ‘prototype’ property points to the object that will be asigned as the prototype of instances created with that function when using ‘new’. Confusing? This is easier to explain with an example:
function Person(name) {
this.name = name;
} // the function person has a prototype property
// we can add properties to this function prototype
Person.prototype.kind = ‘person’ // when we create a new object using new
var zack = new Person(‘Zack’); // the prototype of the new object points to person.prototype
zack.__proto__ == Person.prototype //=> true // in the new object we have access to properties defined in Person.prototype
zack.kind //=> person
That is mostly everything there is to know about the JavaScript object model. Understanding how __proto__ and function.prototype are related will give you countless hours of joy and satisfaction, or maybe not.
Mistakes, confusing? Let me know.
A Plain English Guide to JavaScript Prototypes的更多相关文章
- [转]A plain english introduction to cap theorem
Kaushik Sathupadi Programmer. Creator. Co-Founder. Dad. See all my projects and blogs → A plain engl ...
- [label][JavaScript][The Defined Guide of JavaScript] 如何声明变量
因为觉得我自己的JavaScript基础很不扎实,或者可以说根本就没有所谓基础,所以就最近一直在看<The Defined Guide of JavaScript> . 在一边看的同时,我 ...
- [label][JavaScript][The Defined Guide of JavaScript] 变量的作用域
变量的作用域 一个变量的作用域(scope)是程序中定义这个变量的区域. 全局(global)变量的作用域(scope)是全局性的,即在JavaScript代码中,它处处都有定义. 而在函数之内 ...
- 《JS高程》对象&原型学习笔记
ECMA-262 把对象定义为:”无序属性的集合,其属性可以包含基本值.对象或者函数.”可以把 ECMAScript 的对象想象成散列表:无非就是一组名值对,其中值可以是数据或函数. 6.1.理解 ...
- Cheatsheet: 2019 07.01 ~ 09.30
Other Intro Guide to Dockerfile Best Practices QuickJS Javascript Engine Questions for a new technol ...
- 每个JavaScript开发人员应该知道的33个概念
每个JavaScript开发人员应该知道的33个概念 介绍 创建此存储库的目的是帮助开发人员在JavaScript中掌握他们的概念.这不是一项要求,而是未来研究的指南.它基于Stephen Curti ...
- Tomcat Clustering - A Step By Step Guide --转载
Tomcat Clustering - A Step By Step Guide Apache Tomcat is a great performer on its own, but if you'r ...
- Javascript modules--js 模块化
https://medium.freecodecamp.org/javascript-modules-a-beginner-s-guide-783f7d7a5fcc 这个网站也是非常好:https:/ ...
- JavaScript一词被《牛津大词典》收录了
早上看VS Team的推特发了这个图片,以前总爱问Java怎么读,现在好了,有标准发音了. 确定是 扎瓦·死磕瑞普特 ,哈哈,以后不要再念加瓦了. …… Last month JavaScript r ...
随机推荐
- CMOS (1)–PMOS与NMOS
1,名称来源 p,n指示的是生成的沟道类型 2,驱动逻辑0与逻辑1 一般用NMOS驱动逻辑0,用PMOS驱动逻辑1.
- Lucene基础(四)-- 结合数据库使用
需求 很多时候我们在用数据库的需要使用模糊查询,我们一般会使用like语句来做,然而这样的做的效率不是很多(很抱歉我们亲自去测,很多都这么说的),那么使用Lucene来检索的话,效率会高很多. luc ...
- Spring核心框架 - AOP之动态代理机制
动态代理类的字节码在程序运行时由Java反射机制动态生成,无需程序员手工编写它的源代码.动态代理类不仅简化了编程工作,而且提高了软件系统的可扩展性,因为Java 反射机制可以生成任意类型的动态代理类. ...
- Angular js总结
之前看过一些angular js的相关技术文档,今天在浏览技术论坛的时候发现有问angular js是做什么用的? 于是有了一个想法,对于自己对angular js 的认知做一个总结. 总结: ang ...
- 关于Spring运用过程中jar包报错问题
使用Spring进行web开发时,第一步就是导入jar包,今天使用SPring Task开发定时器时,导入了好多次jar包,都是报错,不知道是因为jar包版本不同还是因为需要依赖的jar包没加入,反正 ...
- 使用WampServer 3.0
在server上安装了WampServer 发现本地使用良好,但是无法从别的PC访问. 原因有二: 1.现象:输入连接无反应 原因:server本身用了80端口,所有WampServer我就设置了80 ...
- 使用text-overflow:ellipsis对溢出文本显示省略号有两个好处
使用text-overflow:ellipsis对溢出文本显示省略号有两个好处,一是不用通过程序限定字数:二是有利于SEO.需要使用对对溢出文本显示省略号的通常是文章标题列表,这样处理对搜索引擎更友好 ...
- Ubutn14.04下caffeine工具不显示在工具栏中的问题
安装过程请参考Ubuntu 14.04下安装Caffeine 2.6.2 阻止显示器进入睡眠状态 至于为什么不显示在任务栏,这不是程序的bug,你可以平ps -e看一下,任务已经在运行. 其实这是新版 ...
- Echarts-JAVA
http://www.oschina.net/p/echarts-java http://my.oschina.net/flags/blog/316920
- Cocos2d-x 3.2编译Android程序错误的解决方案
最近的升级Cocos2d-x 3.2正式版.iOS不管是什么程序编译问题,使用结果cocos compile -p android编译APK计划.结果悲剧,出现以下错误. Android NDK: I ...