class - How to -separately- add functions to classes in Javascript -


this question has answer here:

i have single file looks this:

cars.js:

class cars {      saab(){         return 'saab';     }      volvo(){         return 'volvo';     }  }  var cars = new cars(); 

now want separate them in files organise code:

saab.js

class cars {     saab(){         return 'saab';     } } 

volvo.js

class cars {     volvo(){         return 'volvo';     } } 

cars.js

/**  * magic  */ var cars = new cars(); // contains function volvo() , saab() 

how can this?

ps:
problem extends (class saab extends cars) need call new saab() or new volvo() , that's not want, want call new cars().

kind of odd approach way pull off pre-es6 techniques. namely, extending prototype.

// volvo.js cars.prototype.volvo = function() {   return 'volvo'; };  // saab.js cars.prototype.saab = function() {   return 'saab'; }; 

technically, happens "under hood" class syntax. how has done if don't set of methods in class declaration.


Comments

Popular posts from this blog

Django REST Framework perform_create: You cannot call `.save()` after accessing `serializer.data` -

Why does Go error when trying to marshal this JSON? -