javascript - proper way of adding properties to prototypes in my code -
hey im trying add 'event' , callback function "fun" prototype of myevents , don't understand 1 part of code below!
function myevents(events){ this.events={}; } myevents.prototype.adding=function(event,func){ var array=[] this.array.push(func); this.array.push(event); this.events[event] = array; //i don't understand part-why necessary? }
could please explain? thank you!!
most trying this:
//constructor function function myevents(){ this.events={}; } //extending prototype function called adding myevents.prototype.adding = function(event,func){ this.events[event] = func } //instantiating new object using new operator on constructor function my_new_obj = new myevents() //calling adding function on prototype , passing 2 arguments my_new_obj.adding('test',function greet(){console.log('hello')}) //calling function added my_new_obj['events']['test']()
also, note can powerful because methods (functions) add prototype can access instantiated my_new_obj properties.