JS Notes: Prototype Chain & Inheritance

Thanks a lot for knowledge sharing from EnixJin, an experienced developer with ten years. The series could be split into two parts: JS core concepts(Prototype Chain & Inheritance) and module patterns(CommonJS & AMD &UMD). Here I made a summary with my ideas.

  • Basic Concepts
  • Details with an Example
  • Conclusions

Basic Concepts

JavaScript indeed is a bit confusing for those developers experienced in OOP(object-oriented programming) languages(e.g. C++, java) . The key and hardest part is its different method for inheritance, as it is dynamic and does not provide a class (the class keyword is introduced in ES6(ES2015), but is syntactical sugar, JavaScript remains prototype-based).

It's important to understand the differences between these two, as they are not equivalent, and lead to much confusion down the line. 

When it comes to inheritance, JavaScript only has one construct: objects.    {}

  • A collection of properties
  • Each property has a value
  • A value can be a number, string, boolean, object or function
  • Only null and undefined are not objects

How to create an object in JS?

//version 1: Using an object initializer {}
// create an empty object
var emptyPerson = {};
// create an object with properties
var k = {
    sex: "female",
    age: 18,
    getComments: function() { return "beauty"; },
    "engineer": true,
    occupation: {
        address: "China"
    }
};


//version 2: Using a constructor function (new keyword)
// create an empty object
var person = new Object();
// define an object constructor
function Person(name, age) {
    this.name = name;
    this.age = age;
}
// create an object
var k = new Person("Soda", 18);   


//version 3: Using Object.create()
// create an empty object
var emptyPerson = Object.create(Object.prototype);
// define an object with default properties
var person = {
    name: "Soda",
    age:   18
}
// create an object
var k = Object.create(person);

Details with an Example

Let's take the most familiar constructor, version 2, as an example.

//version 2: Using a constructor function (new keyword)
// define an object constructor
function Person(name, age) {
    this.name = name;
    this.age = age;
}
// create an object
var k1 = new Person("Soda", 18);    //instantiate an object, prototype inheritance was point to Person function 
var k2 = new Person("Jean", 20);    //instantiate another object 


//case 1: adding a property for an object won't affect other instance objects.
//Explanations: This was because every instance object, a copy and prototype inherited from prototype object, is independent.
k1.region = "China";
console.log(k1.region);     //The result is "China" 
                            //Explanations: read from k1 itself property   

console.log(k2.region);     //The result is "undefined"
                            //Explanations:  
                            //read from k2 itself properties, couldn't find it.
                            // try to find its prototype, Person function, couldn't find it.
                            // try to find function its prototype, couldn't find it.
                            //javascript would try to upwards lookup along the prototype chain until the prototype is null(null doesn't have prototype). 


//case 2: add prototype property in prototype object would affect all instance objects.  
Person.prototype.region = "Shanghai";
console.log(k1.region);      //The result is "China"
                             //Explanations: read from k1 itself properties

console.log(k2.region);      //The result is "Shanghai"
                             //Explanations: read from k2 itself properties, couldn't find so try to find its prototype, get it.

 

Conclusions

The pitfalls of prototype inheritance are usually hard to figure out, especially in large complex code structure. So far, most common cases I faced are listed above. Please be careful to modify prototype object.

All implementations are based on this prototype inheritance mechanism. From my perspective, knowing mechanism is much more important than learning implementation. If there are any questions, please feel free to write in comments. Next, we would talk about module patterns.


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
[[Prototype]] is a property that exists on all objects in JavaScript. It is an internal link to another object, known as the object's prototype. In other words, every object in JavaScript inherits properties and methods from a prototype object, which is used as a blueprint for the object's behavior. When a property or method is called on an object, JavaScript first checks if the object itself has that property or method. If it doesn't, it looks up the prototype chain to see if the object's prototype has it. This prototype chain continues until the root object, Object.prototype, is reached. For example, consider the following code: ``` let person = { name: "John", age: 30, sayHello: function() { console.log("Hello!"); } }; let student = { major: "Computer Science" }; student.__proto__ = person; console.log(student.name); // "John" student.sayHello(); // "Hello!" ``` In this example, we create a person object with a name, age, and sayHello method. We then create a student object and set its prototype to be the person object. This means that the student object will inherit the properties and methods of the person object. When we try to access the `name` property of the student object, JavaScript first checks if the student object has a `name` property. Since it doesn't, it looks up the prototype chain and finds the `name` property on the person object. Similarly, when we call the `sayHello` method on the student object, JavaScript looks up the prototype chain and finds the `sayHello` method on the person object. Overall, [[Prototype]] is a powerful feature in JavaScript that allows for efficient and flexible object inheritance.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值