javascript - How to iterate over an object's prototype's properties -


i have code:

var obj = function() { }; // functional object obj.foo = 'foo'; obj.prototype.bar = 'bar';  (var prop in obj) {     console.log(prop); } 

what surprised me logged foo. expected loop iterate on properties of obj's prototype (namely bar), because did not check hasownproperty. missing here? , there idiomatic way iterate on properties in prototype well?

i tested in chrome , ie10.

thanks in advance.

you're iterating on constructor's properties, have create instance. instance inherits constructor's prototype property:

var ctor = function() { }; // constructor function ctor.prototype.bar = 'bar'; var obj = new ctor(); // instantiation  // adds own property instance obj.foo = 'foo';  // logs foo , bar (var prop in obj) {     console.log(prop);  } 

Comments

Popular posts from this blog

c# - How Configure Devart dotConnect for SQLite Code First? -

java - Copying object fields -

c++ - Clear the memory after returning a vector in a function -