- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在一本书中偶然发现了一段话,上面写着:
The hasOwnProperty() technique also requires the property name as a string and will check if an object has the property directly on that instance (and thus is not just an inherited prototype property).
好的,上面的行没有问题。
同一章后面的几行,
由于 JavaScript 是一种动态语言,我们可能会发现修改原生对象的原型(prototype)非常有用,尽管有点危险。
A very important note of warning should be stated here. The practice of extending the prototypes of native objects is both useful and dangerous. For example, it is commonly forbidden to extend the native Object prototype (from which all other objects inherit), useful as that may sound, because doing so causes those properties/methods to be seen in for-in loops that do not properly filter with hasOwnProperty().
当他提出这句话时,他实际上是什么意思,
... causes *those properties/methods to be seen in for-in loops that do not properly filter with ***hasOwnProperty()*...
因为,在上面所示的某个时刻,他说
hasOwnProperty()- will check if an object has the property directly on that instance
它们(原型(prototype)上定义的属性)如何无法正确过滤hasOwnProperty()?
这两种说法是不是很矛盾?
您可以使用这个作为说明,
String.prototype.getThirdChar = function() {
return this.charAt(2);
};
然后,
var c = "Example".getThirdChar(); // c set to "a"
最佳答案
这个例子说明了问题:
// in some file far far away
Object.prototype.foo = function() {};
// in your own code
var obj = { bar: 'bar' };
for (var i in obj) {
console.log(i);
}
记录器将显示两个属性:bar
(预期)和foo
(可能不是,因为修改Object.prototype
的代码通常是隐藏在某些实用程序模块中超出任何合理措施的范围)。原因?就这样:
Objects created from built–in constructors like
Object
have inherited non–enumerable properties fromObject.prototype
. The loop will iterate over all enumerable properties of the object itself and those the object inherits from its constructor's prototype (properties closer to the object in the prototype chain override prototypes' properties).
因此,如果您想迭代对象自己的属性,只需重新检查该属性的所有权即可:
for (var i in obj) if (obj.hasOwnProperty(i)) {
console.log(i); // just 'bar', nothing weird to see here, move along
}
请注意以下内容的区别:
关于javascript - JavaScript 中的动态类型、扩展 Native 和 hasOwnProperty(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32738375/
对版主的澄清由于一些版主在扫描问题时速度有点快,我必须强调我不是在问为什么要使用 Object.prototype.hasOwnProperty.call 而不是 myObject.hasOwnPro
代码一: obj.hasOwnProperty(prop); 代码二: const hasOwnProperty = Object.prototype; hasOwnProperty.call(obj
这个问题在这里已经有了答案: Why use Object.prototype.hasOwnProperty.call(myObj, prop) instead of myObj.hasOwnPro
也许这是一个新手问题,但我找不到或想不出解释。 启动 Node.js 控制台,然后: > global.hasOwnProperty === hasOwnProperty true 那为什么 > gl
在eslint规则中guard-for-in ,直接使用for in是不正确的。好的做法是 for (key in foo) { if (Object.prototype.hasOwnProp
如果我理解正确的话,JavaScript 中的每个对象都继承自 Object 原型(prototype),这意味着 JavaScript 中的每个对象都可以通过其原型(prototype)链访问 ha
我正在尝试让我的某些类型具有某种多重“继承”,如下所示: UIControls.ClickableMesh.prototype = Object.create(THREE.Mesh.prototype
我试图发现一个对象是否具有某些属性并且我在使用 hasOwnProperty 方法时遇到了问题。 我在数组上使用该方法(我知道文档说明了一个字符串)。 以下行返回 true: { "a": 1, "b
谁能解释一下空对象上调用的 hasOwnProperty 的作用?为什么要使用它? __hasProp = {}.hasOwnProperty 我在开始使用 coffescript 进行开发时发现了这
我想使用ES6代理来捕获以下常见代码: for (let key in trapped) { if (!Object.prototype.hasOwnProperty.call(obj, ke
使用 angularjs 和 firebase 编写一个函数,该函数应该检查是否可以在数据库中找到 currentUser。目前“attendings”属性仅包含一个用户,即“Peter Pan” 服
我有一个非常简单的问题。访问对象的属性(例如object[property])和属性的数量是否有任何性能关系?是否存在一些内部循环或其他问题,关于 hasOwnProperty - 任何循环或只是像
我试图理解为什么当我调用下面的函数时结果是“否”,因为属性 c 应该存在。有谁知道为什么?谢谢!!! var letters = function() { this.a = 5; th
在 Javascript 中查找散列中的所有值我看到了以下代码: var myHash = {}; myHash['one'] = 1; myHash['two'] = 2; for (var key
我有以下数据: trace = { "name":"foo", "dataref": { "xdata":"n", "ydata":"m" } }; 我想检查对象是否具有
通过某些值,hasOwnProperty调用将引发错误。 让我们检查以下代码 null.hasOwnProperty('bar') //error undefined.hasOwnProperty('
为什么是真的?对象中的方法foo怎么写? Object.prototype.foo = function(obj) { for(var i in obj) this[i] = obj[i];
这个问题在这里已经有了答案: JavaScript object detection: dot syntax versus 'in' keyword (5 个答案) 关闭 7 年前。 假设有一个对象
问题陈述: 如果对象具有与 JavaScript 预定义的 方法 相同的 property 名称。它无法执行并给出以下错误。 Uncaught TypeError: obj.hasOwnPropert
为什么此函数返回“No Contact”而不是“Akira”? function lookUpProfile(firstName, prop){ for(i=0;i
我是一名优秀的程序员,十分优秀!