javascript - Constructor functions prototype enumerable? -


After reading some posts and documents, I'm still not clear on the actual definition of a calculable asset, moving forward, Let me tell you where I am confused:

I make a constructor function and add a prototype.

  var myAlphabet = function () {this.c = 5 this.d = 6} myAlphabet.prototype = {E: 7}  

now I Using a new keyword, I can create a new example of my alphabet.

  var MyObject = new myAlphabet (); Using the For-in Loop, I want to console.log all the keys in the example of my object (not the key from the prototype).  
  (key in myObj) {console.log (key); }  

For this log:

  for 'c' 'd' 'e'  

According to .in Loop Documentation:

for..in statement repeats on the intentions of an object in arbitrary order. For each separate asset, statements can be executed.

Reading the document for ownership of properties is determined that the asset belongs to the object and not for its prototype series.

So the prototype made earlier is not directly on the example of myObject, it is included in the prototype series. Why is this involved when I am looping on every key?

Why does it include loop on every key?

This is by design of JavaScript objects. Prototype is the way to get those methods

In the same way that if you classes

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

If you start an object type myAlphabet, then there is a property in it A , B and C The difference is that in the languages ​​with the class, in the example, all the values ​​that are "included" are defined by And parents are defined by the class

  class {a: 1 // example because / my parents told me so that c: 2 d: 3} prototype languages In objects, objects are derived from the object, which means that the values ​​are not present in the instance themselves but on the example which serves as a parent  
  object1 {prototype: object2 // hidden fro me enumerator C: 2d: 3 ... // When all the prototypes in the enumerating Get} object2 {Prototype: Ruckle is hidden from the // enumerator: 1 ... // When enumerating is included in the prototype}  

so that you create a legacy virtual Keep it as if it will work with the main difference in the categorized languages, succession is done on the fly .. and if you change the object2, then the prototype objects are actually located in A = new when You read from child alert (object1.a) , Then this will receive new updated values ​​from parent new

if you need to know if an enumerated property exists in the object , Which you are receiving from your parents, then you have object1.hasOwnProperty (a)


Comments

Popular posts from this blog

winforms - C# Form - Property Change -

javascript - amcharts makechart not working -

java - Algorithm negotiation fail SSH in Jenkins -