回调:
b函数作为参数传递到a函数中,a函数执行完后,执行b函数,b函数就叫做回调函数。
function a(callback) { alert("我是parent函数a!"); alert("调用回调函数"); callback(); } function b(){ alert("我是回调函数b"); } function c(){ alert("我是回调函数c"); } function test() { a(b); a(c); }
jq中用到的回调
1、绑定事件
$("p").blur( function () { alert("Hello World!"); } );
$("p").click( function () { $(this).hide(); });
2、效果动作
$("p").show("fast",function(){
$(this).text("Animation Done!");
});
3、ajax操作
$("#feeds").load("feeds.php", {limit: 25}, function(){
alert("The last 25 entries in the feed have been loaded");
});
知乎上相关:http://www.zhihu.com/question/19801131
大神博客地址:http://www.cnblogs.com/yhql/archive/2011/08/08/2131420.html
(不是很理解回调作用!!!)
闭包:
////////////////////////下面是一个闭包////////////////////////// function A(){ //定义一个函数A function B(){ //函数A中定义了函数B console.log('HELLLO WORLD!'); } return B; //在函数A中返回B } var c=A(); //函数A外,定义变量c,执行函数A(),把返回结果赋值给变量c c(); //执行c()
闭包-作用:
javascript具有垃圾回收机制(GC机制),如果一个对象不再被引用,那么这个对象会被GC回收,否则这个对象会被保存在内存中。
在上面例子中,B定义在A中,B依赖于A,而外部变量c应用A,则A不会被GC回收,会保存在内存中。作用如下:
function A(){ var count = 0; function B(){ count++; console.log(count); } return B; } var c =A(); c(); //1 c(); //2 c(); //3
大神博客:http://www.cnblogs.com/onepixel/p/5062456.html