下面有个具体的例子。例子浅而易见,这里同时展示了用函数指针,做钩子进行回调的机制。这东西在java中只能通过接口来实现。 Example: // Parse the text. If it contains any "NaN" strings, replace them
// with the NaN value. All other values are left alone. myData = text.parseJSON(function (value) {
if (value === 'NaN') {
return NaN;
}
return value;
});上面到底在说些什么?parseJSON这个方法是干什么用的,怎么调用这个方法,看不懂啊。。
// with the NaN value. All other values are left alone. myData = text.parseJSON(function (value) {
if (value === 'NaN') {
return NaN;
}
return value;
});上面到底在说些什么?parseJSON这个方法是干什么用的,怎么调用这个方法,看不懂啊。。
分开写就是 fun = function(){...}; text.parseJSON(fun);
parseJSON中一定会定义有调用该函数的代码 fun(); 就是回调机制了。
换句话说,由于fun是变量,定义不同的fun, parseJSON中调用的就是不同的函数。
这在java中就类似于接口:
定义接口
public interface IFunction {
void fun();
}public class Fun1 implements IFunction {
public void fun() {
.......
}
}public class Fun2 implements IFunction {
public void fun() {
.......
}
}public class Text {
public void parseJSON(IFunction function){
function.fun();
}
}调用
Fun1 fun1;
Fun2 fun2;
fun1 = new Fun1();
fun2 = new Fun2();
Text text;
text = new Text();
text.parseJSON(fun1);
text.parseJSON(fun2);
http://www.blogjava.net/emu/archive/2006/08/08/62318.html