Where are a JavaScript's class' instance methods stored? -
this question has answer here:
- es6 iterate on class methods 4 answers
given:
var car = class car{ drive() {} park(){} }
given car, how can find methods class has, ie "drive" , "park" ? have thought on
car.prototype
it's true
car.prototype.drive
returns method ,
car.prototype.hasownproperty("drive") => true
but
for(let prop_name in car.prototype){ console.log(prop_name) }
prints nothing.
the property not enumerable (car.prototype.propertyisenumerable("drive") === false
), therefore doesn't show it. see mdn.
the for...in statement iterates on enumerable properties of object, in original insertion order
otherwise correct, function on prototype.
note not , instance method. instance method exists once per instance, name implies. can generate instance methods e.g. with
class car { constructor() { this.instancefunc = () => {}; } }
note here explicitly new car().instancefunc !== new car().instancefunc
Comments
Post a Comment