Skip to content

Instantly share code, notes, and snippets.

@SIvaCoHan
Created June 28, 2013 15:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SIvaCoHan/5885663 to your computer and use it in GitHub Desktop.
Save SIvaCoHan/5885663 to your computer and use it in GitHub Desktop.
js 闭包问题
function obj(){
return {
init: function(params){
//bla bla bla
},
add: function(x,y){
//blab bla
}
}
}
@kalenderApp
Copy link

严格意义上来说,此函数不算闭包,因为这样写内部函数并没有使用内部函数以外作用域的变量等,改进下。

function obj(params,x,y){

return {
    init: function(){
        // params bla bla bla
    },
    add: function(){
        // x,y blab bla
    }
}

}

这个时候在调用 init 和 add 的时候就算闭包了。

但是 js 中只要是函数,都可以看做是闭包的。

@seekerlee
Copy link

没有自由变量。一般在说“闭包”的时候,都会有自由变量的,不然就直接说“函数”了。@yimity 说的很对。

@halfblood369
Copy link

@seekerlee 精辟的总结啊!没有自由变量,不就是函数么!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment