换句话说,使用这样的代码:
<script>
window['object'] = function(arg) {
var myvar = 1;
this.object = {};
//assign new properties and do more stuff
this.object.x = arg?arg+1:myvar;//just one example for the context
}
//run function once and overwrite it with object
object(1);
</script>
- 功能本身是垃圾收集的吗?
- 它是否会导致对象重用方案,哪些情况本身会减轻创建新对象的成本?
当然,这假定该函数需要包含局部VAR和/或参数依赖项,这证明具有实际函数以首先设置对象。
我要用vyacheslav egorov在Twitter上给出的答案结束这个问题,为Google工作的疯狂俄罗斯编译器工程师,这是自我解释的。
没有真正的性能好处(好的,可以重复使用单个属性,但就是这样)