JavaScript原型及原型链
2024-09-05 14:34:10
代码一:
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<script>
function Car (desc) {
this.desc = desc;
this.color = "red";
} // Car.prototype = {
// getInfo: function() {
// return 'A ' + this.color + ' ' + this.desc + '.';
// }
// };
//instantiate object using the constructor function
var car=new Car("bmw");
// var car = Object.create(Car.prototype);
// console.log(car.getInfo());
console.log(car instanceof Car);
console.log(car.color);
console.log(car.desc);
</script>
</head>
<body> </body>
</html>
以上code输出结果为:
true
red
bmw
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<script>
function Car (desc) {
this.desc = desc;
this.color = "red";
} // Car.prototype = {
// getInfo: function() {
// return 'A ' + this.color + ' ' + this.desc + '.';
// }
// };
//instantiate object using the constructor function
// var car=new Car("bmw");
var car = Object.create(Car.prototype);
// console.log(car.getInfo());
console.log(car instanceof Car);
console.log(car.color);
console.log(car.desc);
</script>
</head>
<body> </body>
</html>
以上code输出结果:
true
undefined
undefined
问题来了:
通过new+构造函数构建实例,实例能访问构造函数的属性
通过Object.create(prototype)构建实例时,实例对象不能访问构造函数的属性
以上结果出现的理论依据在哪里。找出来
最新文章
- raspbian调整分辨率
- 【原创】相对完美的垂直居中popup(modal/dialog),无需监听window.resize事件
- UVA 1151
- Microsoft.Office.Interop.Word.Document.Open returns null on Windows Server 2008 R2
- Redis学习笔记(8)-发布/订阅
- HTML5与移动端Web
- 2016 - 1 -17 GCD主队列与全局队列
- 解决Genymotion下载设备失败的方法(Connection Timeout)
- connect to a specific wifi network in Android programmatically
- PHP解决网站高流量高并发问题
- HDU 5820 Lights(扫描线+zkw线段树)
- 文本框文字垂直居中 CSS
- Elasticsearch集成HanLP分词器-个人学习
- [20180806]tune2fs调整保留块百分比.txt
- Nuget EPPlus的使用
- java常量类编译问题
- HDU 1811 Rank of Tetris(并查集+拓扑排序 非常经典)
- IdentityServer4 密码模式实现
- PLSQL Developer windows 64位连接数据库的问题
- sql performance Kill Lock
热门文章
- android开源项目---View篇
- flex 光标(CursorManager)
- 实例讲解Nginx下的rewrite规则 来源:Linux社区
- 【CS Round #37 (Div. 2 only) D】Reconstruct Graph
- 在OEL 5.4 32bit上使用yum install命令遇到的问题
- 归并排序_分治算法 (白书P226)
- vue --- 全局守卫
- Lucene 定义
- Ubuntu 16.04下的LAMP环境配置
- zabbix3.4.7搭建及邮件告警