Amg*_*hmi 315 javascript
我需要做的就是在当前函数执行结束时执行回调函数.
function LoadData()
{
alert('The data has been loaded');
//Call my callback with parameters. For example,
//callback(loadedData , currentObject);
}
Run Code Online (Sandbox Code Playgroud)
此功能的消费者应该是这样的:
object.LoadData(success);
function success(loadedData , currentObject)
{
//Todo: some action here
}
Run Code Online (Sandbox Code Playgroud)
我该如何实现?
T.J*_*der 565
实际上,您的代码将按原样工作,只需将您的回调声明为参数,您可以使用参数名称直接调用它.
function doSomething(callback) {
// ...
// Call the callback
callback('stuff', 'goes', 'here');
}
function foo(a, b, c) {
// I'm the callback
alert(a + " " + b + " " + c);
}
doSomething(foo);
Run Code Online (Sandbox Code Playgroud)
这将打电话doSomething,这将打电话foo,这将提醒"东西在这里".
请注意,传递函数reference(foo)非常重要,而不是调用函数并传递其result(foo()).在你的问题中,你做得很好,但值得指出,因为这是一个常见的错误.
有时您想要调用回调,以便它看到特定的值this.您可以使用JavaScript call函数轻松完成此操作:
function Thing(name) {
this.name = name;
}
Thing.prototype.doSomething = function(callback) {
// Call our callback, but using our own instance as the context
callback.call(this);
}
function foo() {
alert(this.name);
}
var t = new Thing('Joe');
t.doSomething(foo); // Alerts "Joe" via `foo`
Run Code Online (Sandbox Code Playgroud)
你也可以传递参数:
function Thing(name) {
this.name = name;
}
Thing.prototype.doSomething = function(callback, salutation) {
// Call our callback, but using our own instance as the context
callback.call(this, salutation);
}
function foo(salutation) {
alert(salutation + " " + this.name);
}
var t = new Thing('Joe');
t.doSomething(foo, 'Hi'); // Alerts "Hi Joe" via `foo`
Run Code Online (Sandbox Code Playgroud)
有时,传递您想要将回调作为数组的参数,而不是单独传递是有用的.你可以apply这样做:
function Thing(name) {
this.name = name;
}
Thing.prototype.doSomething = function(callback) {
// Call our callback, but using our own instance as the context
callback.apply(this, ['Hi', 3, 2, 1]);
}
function foo(salutation, three, two, one) {
alert(salutation + " " + this.name + " - " + three + " " + two + " " + one);
}
var t = new Thing('Joe');
t.doSomething(foo); // Alerts "Hi Joe - 3 2 1" via `foo`
Run Code Online (Sandbox Code Playgroud)
小智 75
在尝试执行回调之前,确保回调是实际函数是一种好习惯:
if (callback && typeof(callback) === "function") {
callback();
}
Run Code Online (Sandbox Code Playgroud)
K. *_*erg 57
我2分.相同但不同......
<script>
dosomething("blaha", function(){
alert("Yay just like jQuery callbacks!");
});
function dosomething(damsg, callback){
alert(damsg);
if(typeof callback == "function")
callback();
}
</script>
Run Code Online (Sandbox Code Playgroud)
function loadData(callback) {
//execute other requirement
if(callback && typeof callback == "function"){
callback();
}
}
loadData(function(){
//execute callback
});
Run Code Online (Sandbox Code Playgroud)
function callback(e){
return e;
}
var MyClass = {
method: function(args, callback){
console.log(args);
if(typeof callback == "function")
callback();
}
}
Run Code Online (Sandbox Code Playgroud)
=============================================
MyClass.method("hello",function(){
console.log("world !");
});
Run Code Online (Sandbox Code Playgroud)
=============================================
结果是:
hello world !
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
316964 次 |
| 最近记录: |