events - How to override javascript method and call from class? -
excuse title i'm not sure called (maybe events?):
i have created class use:
function cls_something() { this.notify('hello'); }
now person using class creates method called 'notify' (as instructed me) in order hear notifications , perform own custom code using param pass:
var = new cls_something(); something.notify = function(message) { console.log('the notification ' + message); }
how phone call method within class give him notification message?
fiddle
i'm trying accomplish this...
websocket = new websocket("ws://localhost:10000"); websocket.onopen = function(e) { console.log('you connected'); } websocket.onmessage = function(e) { console.log('omg wtf ffs, there error: ' + e.msg); }
you can phone call this.notify("message"); you'd want check see if it's defined first, though.
edit 1:
ok, problem here you're calling function straight constructor, before it's defined. if needs defined in constructor, pass function parameter.
function cls_something(notifyfunction) { notifyfunction('hello'); }
edit 2:
just we're clear, can have user of class define functions later, if you'd like. can't run them straight constructor, obviously. if run them constructor, need defined before hand.
say class
function cls_something() { this.somefunctionthatisrunlater = function() { this.notify('hello'); } }
then client can write
var = new cls_something(); something.notify = function(message) { console.log('the notification ' + message); }
then, when client calls
something.somefunctionthatisrunlater();
notify called.
javascript events triggers
No comments:
Post a Comment