I'm using the javascript revealing prototype pattern, and I want to add a callback. I'm trying something like:
/
var Refinements = function () { };
Refinements.prototype = function () {
Init = function () {
$('.btn').click(Callback);
},
Callback = function(){
alert('default function');
};
return { Init: Init, Callback : Callback };
}();
var refinements = new Refinements();
refinements.Callback = function(){ alert('new method'); };
refinements.Init();
Essentially what I want to do is pass a callback into the object and raise that callback when an event occurs.
I'm using the javascript revealing prototype pattern, and I want to add a callback. I'm trying something like:
http://jsfiddle/Qyhrb/2/
var Refinements = function () { };
Refinements.prototype = function () {
Init = function () {
$('.btn').click(Callback);
},
Callback = function(){
alert('default function');
};
return { Init: Init, Callback : Callback };
}();
var refinements = new Refinements();
refinements.Callback = function(){ alert('new method'); };
refinements.Init();
Essentially what I want to do is pass a callback into the object and raise that callback when an event occurs.
Share Improve this question edited Jul 20, 2012 at 19:04 gen_Eric 227k42 gold badges303 silver badges342 bronze badges asked Jul 20, 2012 at 19:00 dhysongdhysong 1,0211 gold badge14 silver badges29 bronze badges 4- 1 What problems are you having? What are you expecting this code to do that it doesn't? – Hunter McMillen Commented Jul 20, 2012 at 19:03
- I want the alert('new method') to fire on btn.click instead of the alert('default function'). – dhysong Commented Jul 20, 2012 at 19:05
-
I would prefer a structure like this: jsfiddle/FUBxq/2 - keep the callback function private and overriding it in the
init()
looks much cleaner to me. – Christoph Commented Jul 20, 2012 at 19:33 - @Christoph - Yes, as long as it's not something that might need to be changed later, that would be cleaner. – Scott Sauyet Commented Jul 20, 2012 at 20:25
4 Answers
Reset to default 5 Init = function () {
var refinement = this;
$('.btn').click(refinement.Callback || Callback);
},
Fiddle
var Refinements = function() {};
Refinements.prototype = function() {
return {
init: function() {
$('.btn').click(this.callback);
},
callback: function() {
alert('default function');
}
}
}();
var refinements = new Refinements();
refinements.callback = function() {
alert('new method');
};
refinements.init();
When I separated out the prototype functions, and removed the return { Init: Init, Callback : Callback };
piece everything seems to work fine.
function Refinements() {}
Refinements.prototype.Init = function() {
$('.btn').click(this.Callback);
};
Refinements.prototype.Callback = function() {
alert('default function');
};
var refinements = new Refinements();
refinements.Callback = function(){ alert('new method'); };
refinements.Init();
http://jsfiddle/Qyhrb/8/
Foobar = function() {
this.callBack = function() {
alert("Default method");
};
}
Foobar.prototype = {
Init: function() {
var self = this;
$(".btn").click(function() {
self.callBack.call();
});
}
};
var foobar = new Foobar();
foobar.Init();
foobar.callBack = function() {
alert("Boo!");
};
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742418155a4440146.html
评论列表(0条)