【发布时间】:2011-12-28 13:14:30
【问题描述】:
我正在尝试扩展一些现有功能,使其具有将原始对象作为参数传递的回调函数,或者至少是存储在较小对象中的任何公共变量,我该如何实现?
这是我正在使用的文件的简化版本,一个类将包含在一个变量中
<html>
<head>
</head>
<script>
var foo = new Foo ({
arg1:1,
onComplete: function(){
alert("complete " + total);
}
});
</script>
<body>
</body>
</html>
函数/类看起来类似这样
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();
}
}
})();
}
谢谢
【问题讨论】:
标签: javascript function callback