[Javascript] Link to Other Objects through the JavaScript Prototype Chain

Objects have the ability to use data and methods that other objects contain, as long as it lives on the [prototype] chain. In this lesson we’ll test this out by adding properties and working with this linkage of properties.

 

const obj = {firstName: 'Tyler'};
const protoObj = {lastName: 'Clark'};
Object.setPrototypeOf(obj, protoObj);

 

We can access the 'lastNmae' by calling:

obj.lastName

 

If we set:

obj.lastName = "Wan"

 

So now if we console.log obj.lastName it logs:

obj.lastName // Wan
obj.__proto__.lastName //Clark

 

We we delete:

delete obj.lastName

 

posted @ 2018-10-17 19:53  Zhentiw  阅读(148)  评论(0编辑  收藏  举报