码迷,mamicode.com
首页 > 编程语言 > 详细

JavaScript 对象的值查找

时间:2015-02-12 16:02:04      阅读:154      评论:0      收藏:0      [点我收藏+]

标签:

Javascript对象的值查找规则:

  • 查找顺序
  1. 该对象本身 
  2. 该对象构造函数的prototype
  3. 该对象构造函数的prototype 的构造函数的prototype,以这样的方式沿原型链条逐级查找, 直至Object的prototype
  • 整个查找链条中都没有找到则返回 undefine
  • 查找过程中一旦找到则返回找到的值
 1 Object.prototype.foo = ‘bar‘;
 2 
 3 function People() {}
 4 People.prototype.kind = ‘People‘;
 5 
 6 function Chinese() {}
 7 Chinese.prototype = new People();
 8 Chinese.prototype.constructor = Chinese;
 9 Chinese.prototype.country = ‘China‘;
10 Chinese.prototype.name = ‘Chinese‘;
11 
12 var zhangsan = new Chinese();
13 zhangsan.name = ‘zhang san‘;
14 
15 console.log(zhangsan.name); // zhang san, from zhangsan object,[推测] --> Chinese.prototype中对name的设定被不会被查找
16 console.log(zhangsan.country); // China, from Chinese‘s prototype
17 console.log(zhangsan.kind); // People, from Person‘s prototype
18 console.log(zhangsan.foo); // bar, from Object‘s prototype
19 console.log(zhangsan.age); // undefine

 

JavaScript 对象的值查找

标签:

原文地址:http://www.cnblogs.com/tala/p/4287993.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!