为什么当我在这个JavaScript对象上使用反射时,我可以'看不到在其原型对象中定义的属性
Why when I use reflection on this JavaScript object I can't see a property defined in its prototype object?
我对JavaScript很陌生,实际上我正在教程中学习反射概念。
所以我有以下代码:
/* REFLECTION AND EXTEND:
REFLECTION: An object can look at itself listing and changing its properties and methods. So it means that a JavaScript object have the ability to look at its own properties and methods. We can use this feature of the JavaScript language to implement a very userful pattern called EXTEND.
*/
/* This is a generic person object that have 'firstname' and 'lastname' peropertiest setted on a 'Default' value. This generic person object provide also a getFullName() method that return the concatenation of the firstname and the lastname
*/
var person = {
firstname: 'Default',
lastname: 'Default',
getFullName: function() {
return this.firstname + ' ' + this.lastname;
}
}
/* This john object represent a specific person but do not provide the getFullName() method: */
var john = {
firstname: 'John',
lastname: 'Doe'
}
// don't do this EVER! for demo purposes only !!!
/* This set the 'person' object on the proto variable of the john object. So from this john object I can also use the getFullName() method defined for the generic person object: */
john.__proto__ = person;
// Now, we are going to see an example of the use of reflection in JavaScript:
for (var prop in john) { // Iterate on all the properties into the john object (prop is the current property during the iteration)
if (john.hasOwnProperty(prop)) {
// propo is the current property name and john[prop] is the related value of this property into the john object:
console.log(prop + ': ' + john[prop]);
}
}
代码非常简洁,注释解释了它的作用。
因此,我有一个通用的person对象,它提供了getFullName()方法,还有一个john的对象,它代表一个特定的人,但还没有定义getFullName(。
然后我做:
john.__proto__ = person;
也就是说person对象是john物体的原型,所以这意味着john物件继承自personohn对象可以访问getFullName()方法中的alsto。
然后,我使用反射来迭代john对象,并打印这些对象的所有属性。
我得到了这个结果:
firstname: John
lastname: Doe
这在我看来很奇怪,因为正如我在教程中所看到的,我希望也能看到这个属性:
getFullName: function() {
return this.firstname + ' ' + this.lastname;
}
因为现在我可以访问它。
为什么我看不见?我错过了什么?问题出在哪里?
当你这样做:
john.__proto__ = person;
我认为它连接两个对象,因为Object john
具有person
的原型。因此john可以访问person对象中的所有可用属性。
尽管如此,这并没有将person对象的属性添加到对象john,但它们之间按照__proto__
有一个连接。因此,john仍然有自己的道具,并且可以访问person对象中的方法。
这种情况:
if (john.hasOwnProperty(prop)) {
不允许它记录其原型的属性。
hasOwnProperty
限制对迭代中当前对象的属性访问。
首先要避免使用proto,因为它在所有浏览器中都不一样。现在proto向prototype添加东西,即进行继承。在使用john.hasOwnProperty(prop)时,只对自己的属性为true,而对继承的属性不为true,这就是为什么getFullName
相关文章:
- 为什么我在Internet Explorer上看不到html元素
- ajax请求成功,但可以'我看不到我的数据
- 使用jQuery for RSS看不到多个帖子
- 执行此代码时,我看不到警告框
- Javascript 方法看不到对象变量
- 看不到变量
- Phantomjs 看不到 Twitter/Facebook 授权页面
- AS3 XML上的翻页在IE和FIREFOX上看不到
- JavaScript 看不到声明的变量
- 看不到函数外的Javascript变量
- Javascript对象看不到内部函数
- 为什么我的firefox扩展javascript不能访问opener窗口,也看不到window.name
- 从Node.js查询Neo4j时看不到任何回调
- I'我在css中做了一些隐藏的东西,我可以'I don’我现在点击按钮时看不到它
- 我看不到Javascript数组中的图像
- 硒 IDE 1.8.1 警报框:运行测试时看不到警报框
- 为什么我的 JavaScript 对象看不到以下方法
- 为什么当我在这个JavaScript对象上使用反射时,我可以'看不到在其原型对象中定义的属性
- jQuery没有'看不到新创建的对象.如何刷新DOM对象列表
- 为什么对象看不到他的方法