简体   繁体   English

构造函数原型可枚举?

[英]Constructor functions prototype enumerable?

After reading a few posts and documentation I am still unclear on the true definition of an enumerable property. 在阅读了几篇文章和文档之后,我仍然不清楚可枚举属性的真正定义。 Moving forward, let me show you where I am confused: 继续前进,让我告诉你我困惑的地方:

I create a constructor function and add a prototype. 我创建一个构造函数并添加一个原型。

var myAlphabet = function() {
  this.c = 5
  this.d = 6
}

myAlphabet.prototype = {
  e:7
}

Now I create a new instance of myAlphabet using the new keyword 现在我使用new关键字创建myAlphabet的新实例

var myObject = new myAlphabet();

Using a for-in loop, I want to console.log all the keys in the instance of myObject (not keys from the prototype). 使用for-in循环,我想在console.log中调用myObject实例中的所有键(而不是原型中的键)。

for( key in myObj){
    console.log(key);
}

This logs: 这个日志:

'c'
'd'
'e'

According to the for..in loop documentation: 根据for..in循环文档:

The for..in statement iterates over the enumerable properties of an object, in arbitrary order. for..in语句以任意顺序迭代对象的可枚举属性。 For each distinct property, statements can be executed. 对于每个不同的属性,可以执行语句。

Which makes me believe that the prototype is an enumerable property . 这让我相信原型是一个enumerable property But reading the documentation for Enumerable properties 但是阅读Enumerable properties的文档

Ownership of properties is determined by whether the property belongs to the object directly and not to its prototype chain. 属性的所有权取决于属性是直接属于对象而不属于其原型链。

So the prototype created earlier is not directly on the instance of myObject, it is included in the prototype chain. 因此,之前创建的原型不直接在myObject的实例上,它包含在原型链中。 Why is it including this when I loop over each key? 当我遍历每个键时,为什么包括这个?

Why is it including this when I loop over each key? 当我遍历每个键时,为什么包括这个?

it is by design of the javascript's objects prototype is the way of them inheriting values 它是通过设计javascript的对象原型是它们继承值的方式

the same way that if you had classes 就像你上课一样

class:base
{
    c:2
    d:3
}
base
{
    a:1
}

if you instantiated a object of type myAlphabet it would have properties a,b and c the difference is that in languages with classes the instance would "contain" all the values the ones defined by it and the ones defined by the parent class 如果你实例化了一个类型为myAlphabet的对象,它将具有属性a,b and c ,不同之处在于,在具有类的语言中,实例将“包含”由其定义的所有值以及由父类定义的值。

instance of class
{
    a:1//because my parent told me so
    c:2
    d:3
}

in prototyping languages objects derive from objects which means the values do not resides in the instance itself but on the instance that acts as parent 在原型语言中,对象派生自对象,这意味着值不会驻留在实例本身中,而是驻留在充当父对象的实例上

object1
{
    prototype:object2//hidden from enumerator
    c:2
    d:3
    ...//when enumerating include all from prototype
}
object2
{
    prototype:null//hidden from enumerator
    a:1
    ...//when enumerating include all from prototype
}

so you virtually maintain inheritance just like it would work with classed languages the main difference being, the inheritance is made on the fly.. and the values actually reside in the prototype object if you change object2.a = new when you read from child alert(object1.a) it will fetch the new updated value from the parent new 所以你实际上保持继承就像它与分类语言一样工作,主要区别在于,继承是在运行中进行的..如果你从子alert(object1.a)读取更改object2.a = new ,则值实际上驻留在原型对象中alert(object1.a)它将从父new获取新的更新值

if you need to know if a enumerated property resides in the object itself of is being fetched from the parent you have to use object1.hasOwnProperty(a) 如果您需要知道枚举属性是否位于从父级获取的对象本身中,则必须使用object1.hasOwnProperty(a)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM