JavaScript - Object.create in `Class`ical Inheritance
You've probably seen the Object.create pattern was used to mimic Classical inheritance in JavaScript ES5 such as:
function Animal() {
}
Animal.prototype.say = function() {
console.log('Say!');
};
function Dog() {
}
Dog.prototype = Object.create(Animal.prototype);
Dog.prototype.constructor = Dog;
You may be curious about why Object.create is used here to make inheritance. So let's figure out why it is needed for proper inheritance.
the following example can also be used to mimic inheritance:
function Animal() {
}
Animal.prototype.say = function() {
console.log('Say!');
};
function Dog() {
}
Dog.prototype = new Animal();
Dog.prototype.constructor = Dog;
I think you've already noticed this code can cause a side-effect. Copying prototype with new Animal() can cause side-effect, because it will call Animal constructor once. and it's unnecessary action.
So we need to copy Animal.prototype without calling the constructor. and using Object.create is the answer.
References
Leave JavaScript - Object.create in `Class`ical Inheritance 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분 정도 지났는데 아직 안들어오네요.