Where are a JavaScript's class' instance methods stored? -


this question has answer here:

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

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -