javascript - How can I merge a module in an instance? -


i'm trying find way merge or add methods , attributes of module in class.

function user(data){   this.name = data.name; }  user.prototype.sayhello = function(){   return this.name+' says hello.'; }  module.exports = user; 

i add user attributes , methods player class once has joined game exemple.

function player(user){   this.chances = 3; }  player.prototype.run = function(){  return this.name+' running.'; } 

i tried subclass loose parameters user , have player's one. :

var user = new user({name:jacques});  user.sayhello() // jacques says hello.  user.becomeplayer();  user.run(); // jacques running. 

or possible add attributes , methods user variable ?

user.becomeplayer() // user can run. 

if want add method on object user,you can sth this:

user.prototype =  {     sayhello : function(){         return this.name+' says hello.';     },     becomeplayer: function () {         //do thing     } } 

if user want access player's method, can use inheritance( prototype chain inheritance):

user.prototype = new player(); var user = new user({name: 'jacques'})  console.log(user.run());//jacques running. 

Comments

Popular posts from this blog

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -

python - Error while using APScheduler: 'NoneType' object has no attribute 'now' -