[url=http://www.1sucai.cn/article/62255.htm]关于Javascript的this的一些知识[/url]》一文中所说的。
var name = "The window";
var object = {
name: "My Object",
getNameFunc: function(){
var that = this;
return function(){
return that.name;
}
}
};
object.getNameFunc()()
[b]Javscript 闭包与读写变量
[/b]值得注意的是,如果我们没有处理好我们的变量时,我们也可以修改这些变量。
function hello(){
var char = "hello,world";
return{
set: function(string){
return char = string;
},
print: function(){
console.log(char)
}
}
}
var say = hello();
say.set('new hello,world')
say.print() // new hello world
[b]Javascript 闭包与性能[/b]
引用MDC的说法
如果不是因为某些特殊任务而需要闭包,在没有必要的情况下,在其它函数中创建函数是不明智的,因为闭包对脚本性能具有负面影响,包括处理速度和内存消耗。
文上还说到。
例如,在创建新的对象或者类时,方法通常应该关联于对象的原型,而不是定义到对象的构造器中。原因是这将导致每次构造器被调用,方法都会被重新赋值一次(也就是说,为每一个对象的创建)。