javascript 为什么 hasOwnProperty 不能识别对象原型上的函数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22445261/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why does hasOwnProperty not recognise functions on an object's prototype?
提问by glenatron
I understand that the hasOwnProperty
method in JavaScript exists to identify properties only of the current type, but there is something in the prototype chain here that is confusing to me.
我知道hasOwnProperty
JavaScript中的方法仅用于识别当前类型的属性,但是这里的原型链中有一些东西让我感到困惑。
Let us imagine that I define a type called Bob, and assign two child functions to my Bob type in two different ways:
让我们想象一下,我定义了一个名为 Bob 的类型,并以两种不同的方式为我的 Bob 类型分配两个子函数:
function Bob()
{
this.name="Bob";
this.sayGoodbye=function()
{
console.log("goodbye");
}
}
Bob.prototype.sayHello= function()
{
console.log("hello");
}
Now aside from having access to closure scope in the case of sayGoodbye
, it seems to me that both functions belonging to the Bob
class should be more or less equal. However, when I look for them with hasOwnProperty
they are not the same as far as JavaScript is concerned:
现在除了在 的情况下可以访问闭包范围之外,在sayGoodbye
我看来,属于Bob
该类的两个函数应该或多或少相等。但是,当我使用hasOwnProperty
它们查找它们时,就 JavaScript 而言,它们并不相同:
var myBob = new Bob();
console.log( myBob.name ); // Bob, obviously
console.log( myBob.hasOwnProperty("sayHello")); // false
console.log( myBob.hasOwnProperty("sayGoodbye")); // true
console.log( "sayHello" in myBob ); // true
What is happening here in terms of scope? I could not create an instance of the Bob
type without having the sayHello()
and sayGoodbye()
properties connected to it, so why is the prototype method a second class citizen as far as hasOwnProperty
is concerned? Is Bob.prototype
a type that exists somehow independently of the Bob
type, from which Bob
inherits everything?
就范围而言,这里发生了什么?如果没有和属性连接到它,我无法创建该Bob
类型的实例,那么为什么原型方法是二等公民呢?一种类型是否以某种方式独立于类型而存在,从中继承了所有内容?sayHello()
sayGoodbye()
hasOwnProperty
Bob.prototype
Bob
Bob
采纳答案by elclanrs
I think you're confusing a few concepts here. Let's take this quote from the MDN:
我认为你在这里混淆了一些概念。让我们从MDN 中引用这句话:
Every object descended from
Object
inherits thehasOwnProperty
method. This method can be used to determine whether an object has the specified property as a direct propertyof that object; unlike thein
operator, this method does not check down the object's prototype chain.
每个对象都
Object
继承了该hasOwnProperty
方法。该方法可用于判断一个对象是否具有指定的属性作为该对象的直接属性;与in
操作符不同,此方法不检查对象的原型链。
So that's the key here. When you use new
JavaScript will assign a brand new object to this
and return it, that's what an instance is. Any property declared inside the constructor is an own property. Properties declared on the prototype
are not, since they are shared with other instances of the same object.
所以这是这里的关键。当您使用new
JavaScript 时,会为其分配一个全新的对象this
并返回它,这就是实例。在构造函数中声明的任何属性都是自己的属性。在 上声明的属性prototype
不是,因为它们与同一对象的其他实例共享。
And a prototype
is also an Object
, for example:
并且 aprototype
也是 an Object
,例如:
Bob.prototype.hasOwnProperty("sayHello"); //=> true
myBob.constructor.prototype.hasOwnProperty("sayHello"); //=> true
回答by RobG
I understand that the hasOwnProperty method in JavaScript exists to identify properties only of the current type
我了解 JavaScript 中的 hasOwnProperty 方法仅用于识别当前类型的属性
That is not correct. hasOwnPropertyidentifies ownproperties of an object, that is, properties of the object itself. It does not consider inherited properties on an object's [[Prototype]]
chain.
那是不正确的。hasOwnProperty识别自己的一个对象,即,对象本身的属性的属性。它不考虑对象[[Prototype]]
链上的继承属性。
e.g.
例如
var foo = {name: 'foo'};
// Check for an own property
foo.hasOwnProperty('name'); // true
The object fooalso inherits a toStringmethod from Object.prototype, but it's not an "own" property:
对象foo也从 Object.prototype继承了一个toString方法,但它不是“自己的”属性:
typeof foo.toString // function
foo.hasOwnProperty('toString'); // false