javascript 函数式编程


JavaScript的函数式编程的对象本质:
function a()
{
var x="sth";
return b(){
//do with x;
}
}
var c = a();
等价于
function a()
{
this.x = "dosth";
this.b = function(){
//do with this.x
}
}
var c = new a();

但是
1)函数式编程本能地避免无用对象的构造
因为函数式编程鼓励当用到闭包的时候才去调用闭包构造
而面向对象通常事先构造好对象,准备在那里
2)函数式编程保护私有变量
x不会被除闭包外的外部访问
3)闭包和对象一样要注意释放资源
如: c = null; setTimeout(CollectGarbage,100);
« 
» 
快速导航

Copyright © 2016 phpStudy | 豫ICP备2021030365号-3