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 -

reflection - How to access the object-members of an object declaration in kotlin -

php - Doctrine Query Builder Error on Join: [Syntax Error] line 0, col 87: Error: Expected Literal, got 'JOIN' -