JavaScript class define a callback delegate method
I recently working on a JavaScript project, which required to write class.
Usually I just use 3rd party plugin for most of my project, e.g. jQuery.ajax
1 | $.ajax({ |
something like that, but when I try to create a class which has a method like success
in jQuery.ajax
, to let the user who use my library to be able to handle the callback. So here is what I found:
FooBar.js
Your defination goes here
1 | (function() { |
script.js
Your implementation goes here
1 | var foobar = new FooBar({ |
If let say you want to pass extra params
1 | this.options.close.call(this, 'extra message'); |
References: