JavaScript - Understanding Prototype
There is a very hard to understand concept which JavaScript has called Prototype. You've already seen a lot if you have an experience with JavaScript.
Many books and articles describing prototype are out there, but i just write this yet another article about prototype to improve my understanding.
Actually many people calls both Prototype Object and Prototype Link a Prototype. So it becomes beginners to understand difficult.
So let's figure out the differences between Prototype Object and Prototype Link.
Prototype Object vs Prototype Link
When you declare a Function, then the Function object have its own Prototype Object.
the Prototype Object is just another plain object, and it has the parent Function on its constructor property. (Circular Reference)
function Animal() {
}
console.log(Animal.prototype.constructor === Animal); // prints true
And when you instantiate new object with the Function, the instantiated object will be linked with the Prototype Object just like this:
function Animal() {
}
Animal.prototype.say = function() {
console.log('this is animal');
};
var animal = new Animal();
console.log(animal.__proto__ === Animal.prototype); // prints true
You may notice that instantiated animal object has strange __proto__ property. and Yes, __proto__ is a Prototype Link.
How Prototype works
Prototype Object is just a special plain object which all Function objects have.
and Prototype Link is a just a link to original Prototype Object.
With Prototype Link, You can call a function which is located at the Prototype Object on the instantiated object, for example:
function Animal() {
this.name = 'animal';
}
Animal.prototype.say = function() {
console.log('This is ' + this.name);
};
function Dog() {
this.name = 'dog';
}
Dog.prototype = Object.create(Animal.prototype);
Dog.prototype.constructor = Dog;
var dog = new Dog();
dog.say(); // call Animal.prototype.say with 'dog' context, prints 'This is dog'
In the example, when you call a say function on dog object. JavaScript engine will find say function on the dog object first, and if can't find it then it will find the function on the its __proto__ object, and so on. and finally the found function is called with dog object context. (its like: Dog.__proto__.say.call(dog);)
And finding the calling function can be following multiple prototype links, it's called Prototype Chain.
the following figure is describing the prototype relations for the above example:
Summary
- Prototype Object: a Special plain object on all Function objects
- Prototype Link: a Link to the Prototype Object on all objects
References
And all my writings are also available on http://appetizermonster.github.io/blog
Leave JavaScript - Understanding Prototype to:
Read more #programming posts
Best Posts From Heejin
We have not curated any of heejin's posts yet. But you can encourage our curation team to review posts by visiting them regularly and by referring other readers. Because we give priority to frequently read content.
More Posts From Heejin
- DELETED
- 개발자들을 위한 큐레이션 사이트를 만들어봤습니다.
- SteemConnect가 Steem용 dApp의 발전을 저해하고 있는 것 같습니다.
- Binance Chain, 바이낸스에서 자신들만의 블록체인을 만든다고 하네요
- 지금까지 스팀잇에 대한 환상을 가지고 있지 않았을까
- BTS(BitShares)가 Bittrex에서 곧 상장 폐지됩니다.
- Coinhive - 당신의 블로그로 수익을 만드는 새로운 방법 (블록체인 기반)
- JavaScript - hasOwnProperty in `for-in` loops
- JavaScript - Understanding Prototype
- JavaScript - Object.create in `Class`ical Inheritance
- Learning How to Learn
- JavaScript - var, let, const
- 정렬 알고리즘 - 거품 정렬 (Bubble sort)
- [이벤트 참여] @kimdy 님의 프로그램을 다양한 언어로
- 원어민과의 온라인 영어회화 수업 후기
- 뉴비의 스팀잇 접근을 어렵게 만드는 요소!
- If you use Facebook React, Take a look at the LICENSE carefully!
- Facebook의 React를 사용할 때 주의해야 할 라이센스 문제
- 자기계발 수단으로써의 스팀잇
- 폴로닉스에 스팀 달러를 전송했습니다, 10분 정도 지났는데 아직 안들어오네요.