代码之家  ›  专栏  ›  技术社区  ›  Gordon Williams

闭包:为什么对象没有内联?

  •  0
  • Gordon Williams  · 技术社区  · 4 年前

    我在闭包编译器上遇到了一个问题——有时它会内联结构的内容,有时不会。例如:

    (function(){
      const C = {SOMETHING: 0x76, BLA: 123}
    
      function Test(write) {
        write(5+C.BLA);
      }
    
      exports.hello = function() {
        return Test;
      };
    })()
    

    (function(){function a(b){b(5+c.BLA)}var c={SOMETHING:118,BLA:123};exports.hello=function(){return a}})();
    

    只是改变了方式 Test

    (function(){
      const C = {SOMETHING: 0x76, BLA: 123}
    
      function Test(write) {
        write(5+C.BLA);
      }
    
      exports.hello = Test;
    })()
    

    将导致其内联罚款:

    (function(){exports.hello=function(a){a(128)}})();
    

    (function(){
      /** @enum {number} */
      const C = {/** @const */SOMETHING: 0x76, /** @const */BLA: 123}
    
      function Test(write) {
        write(5+C.BLA);
      }
    
      exports.hello = function() {
        return Test;
      };
    })()
    

    知道为什么它不会内联吗?有没有办法从编译器那里得到一个报告或发现呢?

    0 回复  |  直到 4 年前