什么更有效,将私有X私有变量存储在对象中或将它们用作闭包?

时间:2016-05-31 18:05:27

标签: javascript performance variables object closures

我想知道什么是更好的 想象一下,在库中我有一个存储一些变量的对象。其中一些在语言上或上下文中不适合该变量,但它们用于该对象中的某些函数。 最好将它们作为私有变量存储在该对象中,如果我希望它们不被这些函数使用,或者在IIFE中外部声明它们来准备库并将这些变量用作闭包?

想象:

let func = (function(){
  let func = function(){
     let a = "hello";
     this.sayHello = function(){
        alert(a);
     };
  }

  //Extend object etc and do some other things.
  return func;
})();

OR

let func = (function(){
  let a = "hello";

  let func = function(){
     this.sayHello = function(){
        alert(a);
     };
  }

  //Extend object and do some other things.
  return func;
})();

0 个答案:

没有答案