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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 23:10:41  来源:igfitidea点击:

Why does hasOwnProperty not recognise functions on an object's prototype?

javascriptprototypehasownproperty

提问by glenatron

I understand that the hasOwnPropertymethod 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.

我知道hasOwnPropertyJavaScript中的方法仅用于识别当前类型的属性,但是这里的原型链中有一些东西让我感到困惑。

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 Bobclass should be more or less equal. However, when I look for them with hasOwnPropertythey 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 Bobtype without having the sayHello()and sayGoodbye()properties connected to it, so why is the prototype method a second class citizen as far as hasOwnPropertyis concerned? Is Bob.prototypea type that exists somehow independently of the Bobtype, from which Bobinherits everything?

就范围而言,这里发生了什么?如果没有和属性连接到它,我无法创建该Bob类型的实例,那么为什么原型方法是二等公民呢?一种类型是否以某种方式独立于类型而存在,从中继承了所有内容?sayHello()sayGoodbye()hasOwnPropertyBob.prototypeBobBob

采纳答案by elclanrs

I think you're confusing a few concepts here. Let's take this quote from the MDN:

我认为你在这里混淆了一些概念。让我们从MDN 中引用这句话:

Every object descended from Objectinherits the hasOwnPropertymethod. This method can be used to determine whether an object has the specified property as a direct propertyof that object; unlike the inoperator, this method does not check down the object's prototype chain.

每个对象都Object继承了该hasOwnProperty方法。该方法可用于判断一个对象是否具有指定的属性作为该对象的直接属性;与in操作符不同,此方法不检查对象的原型链

So that's the key here. When you use newJavaScript will assign a brand new object to thisand return it, that's what an instance is. Any property declared inside the constructor is an own property. Properties declared on the prototypeare not, since they are shared with other instances of the same object.

所以这是这里的关键。当您使用newJavaScript 时,会为其分配一个全新的对象this并返回它,这就是实例。在构造函数中声明的任何属性都是自己的属性。在 上声明的属性prototype不是,因为它们与同一对象的其他实例共享。

And a prototypeis 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