I just wondered if something like this can be achieved:
Assume that I have a event callback inside my library defined:
var app = {
globals: {
foo: null
},
bar: function(e) {
e.preventDefault();
e.stopPropagation();
if (typeof app.globals.foo === "function") app.globals.foo(e);
},
init: function(options) {
$.extend(app.globals, options);
$(".myclass").on("click", function(e) {
app.bar(e);
});
}
};
$(document).ready(function() {
app.init({
foo: function(evt) {
alert("clicked");
console.log(evt.target);
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="myclass">Click Me!</div>
With this method, I'm passing the ".myclass" elements' click event data to app.foo, so the object oriented method would be executed.
TL;DR: The question is, can I set the scope of foo as the object which sent the click event? So:
app.init({
foo: function(){
console.log(this);
}
});
would be executed same as:
app.init({
foo: function(evt){
console.log(evt.target);
}
});
Is something like this possible?
app.foo?)