【问题标题】:is it a good way to return another function in javascript?这是在javascript中返回另一个函数的好方法吗?
【发布时间】:2013-01-25 08:13:38
【问题描述】:

在阅读一些 javascript 源代码时,我发现以下代码:

var fullname = function(){
     var shorts = { pos: "position", w: "width", h: "height", l: "left", t: "top" };
        return function(name){
          return shorts[name] || name;
      }
 }();

返回另一个函数是更有效还是其他原因? 为什么不使用:

    function fullname(name){
         var shorts = { pos: "position", w: "width", h: "height", l: "left", t: "top" };
         return shorts[name] || name; 
  }

【问题讨论】:

    标签: javascript function return


    【解决方案1】:

    这相当于

    var shorts = { pos: "position", w: "width", h: "height", l: "left", t: "top" };
    
    function fullname(name){
          return shorts[name] || name;
    }
    

    ... 在它的作用中。但是

    var fullname = function(){
         var shorts = { pos: "position", w: "width", h: "height", l: "left", t: "top" };
            return function(name){
              return shorts[name] || name;
          }
     }();
    

    ... 确保哈希/对象 shorts,* private 仅对函数 fullname* .

    所以回答你的问题,为什么不呢

      function fullname(name){
             var shorts = { pos: "position", w: "width", h: "height", l: "left", t: "top" };
             return shorts[name] || name; 
      }
    

    这里'shorts 隐藏在 fullname 中,但正如 Dogbert 指出的那样,它更慢 因为哈希 shorts在每次调用时创建的

    但这提供了两全其美:

    var fullname = function(){
         var shorts = { pos: "position", w: "width", h: "height", l: "left", t: "top" };
            return function(name){
              return shorts[name] || name;
          }
     }();
    

    shortsfullname 保持私有 同时,shorts 只被实例化一次,所以性能会 做个好人。

    这就是他们所说的IIFE(立即调用函数表达式)。这个想法是 在函数B 内创建一个函数A 并声明用于函数A 的变量 在函数 B;s 范围内,因此只有函数 A 可以看到它们。

     var B = function(){
        // this is B
        // 
        val foo = {x:1}; // this is ONLY for A
    
        val A = function(arg0) {
           // do something with arg0, foo, and whatever
           // and maybe return something
        };
    
        return A;
     };
    
     val A = B();
    

    如你所见,和

     var A = (function(){
        // this is in 'B'
        // 
        val foo = {x:1}; // this is ONLY for A
    
        return function(arg0) { // this is in A
           // do something with arg0, foo, and whatever
           // and maybe return something
        };
    
     })(/*calling B to get A! */);
    

    在功能上,这完全一样

     val foo = {x:1};  // foo is visible OUTSIDE foo!
    
     val A = function(arg0) {
        // do something with arg0, foo, and whatever
        // and maybe return something**strong text**
     };
    

    我没有看到任何其他用途。 所以这只是保持变量私有的一种方式,并且同时, 不会失去性能。

    (见What is the (function() { } )() construct in JavaScript?

    【讨论】:

    • 我想补充一点,这使得第一段代码快得多,因为变量只声明了一次,而不是第二段。检查我创建的this benchmark。 (我在 FF 中快了大约 25 倍)。
    • @Dogbert - 感谢您指出这一点。哇,不知何故,像你这样的名字,我忍不住顺从你说的任何话......;-)
    • 很好,看了回答我很明白。顺便说一句,@Dogbert 的例子太棒了。干得好!
    【解决方案2】:

    在另一个函数中返回一个函数会导致创建闭包。在这种情况下,内部函数将可以访问外部函数范围,其中包括对象 shorts

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-06
      • 1970-01-01
      • 1970-01-01
      • 2013-11-05
      • 1970-01-01
      • 1970-01-01
      • 2012-07-20
      • 1970-01-01
      相关资源
      最近更新 更多