I am trying to extend some existing functionality so it has a callback function that passes the original object as a parameter, or at least any public variables that is stored in a smaller object, how can I achieve this?
This is a simplified version of the file I am working with, a class would be contained in a variable
<html>
<head>
</head>
<script>
var foo = new Foo ({
arg1:1,
onComplete: function(){
alert("complete " + total);
}
});
</script>
<body>
</body>
</html>
The function / class looks similar to this
function Foo(options) {
// private
var number1_ = options.arg1;
var number2_ = 10;
var total_ = number1_ + number2_;
// public
this.total = total_;
(function Foo() {
if (options) {
if (options.onComplete && typeof (options.onComplete) === "function") {
// how do I pass Foo or an object of selected variables here?
options.onComplete();
}
}
})();
}
Thanks
Check this out 🙂
Live demo:
http://jsfiddle.net/P3bzM/