i'm trying to create a simple class to emit an event in certain moment. I get this javascript error : this.emit is not a function
This is the code:
var class_test = function(){};
class_test.prototype = {
some_property: null,
doSomething: function(msg) {
this.some_property = msg;
this.emit("somethingHappened");
}
};
var test = new class_test();
test.doSomething('Example');
test.addEventListener('somethingHappened',function(){
alert("Event");
},false);
Any suggestion?,Thanks!
i'm trying to create a simple class to emit an event in certain moment. I get this javascript error : this.emit is not a function
This is the code:
var class_test = function(){};
class_test.prototype = {
some_property: null,
doSomething: function(msg) {
this.some_property = msg;
this.emit("somethingHappened");
}
};
var test = new class_test();
test.doSomething('Example');
test.addEventListener('somethingHappened',function(){
alert("Event");
},false);
Any suggestion?,Thanks!
Share Improve this question asked Mar 14, 2014 at 14:31 hanskaithanskait 451 silver badge9 bronze badges 4- 2 That code is not using jQuery at all. – Frédéric Hamidi Commented Mar 14, 2014 at 14:32
- @FrédéricHamidi the class is wrong? – hanskait Commented Mar 14, 2014 at 14:38
-
@hanskait: Do you know what jQuery is? And
emit()
es from NodeJS. Is that what you're using? – cookie monster Commented Mar 14, 2014 at 14:40 -
1
@hanskait, well, it does not have the
emit()
method you're trying to call. If you wish to emit events with jQuery, I would suggest you use the library (on()
instead ofaddEventListener()
for instance) and refer to the trigger() method. – Frédéric Hamidi Commented Mar 14, 2014 at 14:41
1 Answer
Reset to default 5with jQuery ,since you requested it :
var class_test = function(){
this.dispatcher = $({});
};
class_test.prototype = {
some_property: null,
doSomething: function(msg) {
this.some_property = msg;
this.dispatcher.trigger("somethingHappened");
}
};
var test = new class_test();
test.dispatcher.on('somethingHappened',function(){
alert("Event");
});
test.doSomething('Example');
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742318114a4421251.html
评论列表(0条)