[Javascript] Link to Other Objects through the JavaScript Prototype Chain (Object.setPrototypeOf())
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.
Objects in JavaScript give us the ability to create key and value pairs. In this example, we've got an object here called obj with the firstName property. We console.log firstName. We see that we get Tyler.
const obj = {
firstName: 'Tyler'
}
console.log(obj.firstName) // Tyler
As you probably expect, if we were to look up a property that did not exist on our object, we'll get an undefined back, which is true for all cases where the property is not on the object, unless that property lives somewhere on the prototype chain.
const obj = {
firstName: 'Tyler'
}
console.log(obj.lastName) // undefined
But you still able to get toString()
method, that's is because toString()
exists on prototype chain, __proto__
.
Now, let's create a new object called protoObj. We'll give it the property of lastName, with the value of string of Clark, then we'll say Object.setPrototypeOf(obj, protoObj)
As the name suggests, we are setting the prototype or the next inline chain object of object to be proto object.
const obj = {
firstName: 'Tyler'
}
const protoObj = {
lastName: 'Clark'
}
Object.setPrototypeOf(obj, protoObj)
console.log(obj.lastName) // Clark
相关文章
- javascript 高级教程 视频_精通JavaScript
- Maven报错:The packaging for this project did not assign a file to the build artifact[通俗易懂]
- javascript中js实现导出CSV文件功能
- JavaScript生成验证码和32位随机码
- To add browser notifications to an online customer support live chat service system - gofly - self h
- ORA-26748: The one-to-one transformation function string encountered the following error: string ORACLE 报错 故障修复 远程处理
- ORA-27611: Smart I/O failed due to a block corruption detected on the host. The block was received from cell “string”. disk: “string”, block: “string”, disk offset: “string” ORACLE 报错 故障修复 远程处理
- ORA-30158: The OCIFileWrite causes the file to exceed the maximum allowed size ORACLE 报错 故障修复 远程处理
- ORA-32342: The EXPLAIN_MVIEW facility failed to explain the materialized view statement ORACLE 报错 故障修复 远程处理
- JavaScript学习总结(二十)——Javascript非构造函数的继承详解编程语言
- JavaScript学习总结(十七)——Javascript原型链的原理详解编程语言
- JavaScript学习总结(九)——Javascript面向(基于)对象编程详解编程语言
- javascript window获取iframe、iframe获取window、document详解编程语言
- the cloudTaking Oracle to the Cloud: The Evolution of Database Systems(oracleisin)
- 妙用JavaScript调用Oracle函数(js调用oracle函数)
- 将光标定位到textarea的某一行的javascript代码
- 一个可以得到元素真实的背景颜色的javascript脚本
- JavaScript入门教程(10)认识其他对象
- 正确使用带有"g"标记的javascript正则表达式
- JavaScript设计模式富有表现力的Javascript(一)
- Javascript中的this绑定介绍
- JavaScript高级程序设计阅读笔记(十六)javascript检测浏览器和操作系统-detect.js
- javascript中负数算术右移、逻辑右移的奥秘探索
- JavaScript自定义日期格式化函数详细解析