Dart的输出是什么样的?

Dan*_*ite 32 javascript dart

Google的新网络语言Dart表示它支持输出到JavaScript.

简单的转换是什么样的?

Sim*_*ris 46

main() {
      print('Hello, Dart!');
}
Run Code Online (Sandbox Code Playgroud)

使用dart2js编译时(截至2013-04-26)(见底部注释),将其转换为:

// Generated by dart2js, the Dart to JavaScript compiler.
// The code supports the following hooks:
// dartPrint(message)   - if this function is defined it is called
//                        instead of the Dart [print] method.
// dartMainRunner(main) - if this function is defined, the Dart [main]
//                        method will not be invoked directly.
//                        Instead, a closure that will invoke [main] is
//                        passed to [dartMainRunner].
function Isolate() {}
init();

var $ = Isolate.$isolateProperties;
// Bound closures
$.Primitives_printString = function(string) {
  if (typeof dartPrint == "function") {
    dartPrint(string);
    return;
  }
  if (typeof window == "object") {
    if (typeof console == "object")
      console.log(string);
    return;
  }
  if (typeof print == "function") {
    print(string);
    return;
  }
  throw "Unable to print message: " + String(string);
};

$.main = function() {
  $.Primitives_printString("Hello, Dart!");
};

$.String = {builtin$cls: "String"};

var $ = null;
Isolate = Isolate.$finishIsolateConstructor(Isolate);
var $ = new Isolate();
// BEGIN invoke [main].
if (typeof document !== "undefined" && document.readyState !== "complete") {
  document.addEventListener("readystatechange", function () {
    if (document.readyState == "complete") {
      if (typeof dartMainRunner === "function") {
        dartMainRunner(function() { $.main(); });
      } else {
        $.main();
      }
    }
  }, false);
} else {
  if (typeof dartMainRunner === "function") {
    dartMainRunner(function() { $.main(); });
  } else {
    $.main();
  }
}
// END invoke [main].
function init() {
  Isolate.$isolateProperties = {};
  Isolate.$finishIsolateConstructor = function(oldIsolate) {
    var isolateProperties = oldIsolate.$isolateProperties;
    isolateProperties.$currentScript = typeof document == "object" ? document.currentScript || document.scripts[document.scripts.length - 1] : null;
    var isolatePrototype = oldIsolate.prototype;
    var str = "{\n";
    str += "var properties = Isolate.$isolateProperties;\n";
    var hasOwnProperty = Object.prototype.hasOwnProperty;
    for (var staticName in isolateProperties) {
      if (hasOwnProperty.call(isolateProperties, staticName)) {
        str += "this." + staticName + "= properties." + staticName + ";\n";
      }
    }
    str += "}\n";
    var newIsolate = new Function(str);
    newIsolate.prototype = isolatePrototype;
    isolatePrototype.constructor = newIsolate;
    newIsolate.$isolateProperties = isolateProperties;
    return newIsolate;
  };
}
//@ sourceMappingURL=out.js.map
Run Code Online (Sandbox Code Playgroud)

后人的注意事项:对此问题的原始答案进行了修改,以反映当前的事态.

在2012-05-12,Hello World的dart输出为18,718个字符.

在2012-08-29,输出为1531个字符.

在2013-04-26,输出为2642个字符.

dart2js可以缩小代码.这是一个例子(截至2013-04-26)

// Generated by dart2js, the Dart to JavaScript compiler.
function I(){}
init()
var $=I.p
$.ib=function(a){if(typeof dartPrint=="function"){dartPrint(a)
return}if(typeof window=="object"){if(typeof console=="object")console.log(a)
return}if(typeof print=="function"){print(a)
return}throw "Unable to print message: " + String(a)}
$.E2=function(){$.ib("Hello, Dart!")}
$.qU={builtin$cls:"qU"}

var $=null
I = I.$finishIsolateConstructor(I)
var $=new I()
if (typeof document !== "undefined" && document.readyState !== "complete") {
  document.addEventListener("readystatechange", function () {
    if (document.readyState == "complete") {
      if (typeof dartMainRunner === "function") {
        dartMainRunner(function() { $.E2(); });
      } else {
        $.E2();
      }
    }
  }, false);
} else {
  if (typeof dartMainRunner === "function") {
    dartMainRunner(function() { $.E2(); });
  } else {
    $.E2();
  }
}
function init(){I.p={}
I.$finishIsolateConstructor=function(a){var z=a.p
z.$currentScript=typeof document=="object"?document.currentScript||document.scripts[document.scripts.length-1]:null
var y=a.prototype
var x="{\n"
x+="var properties = I.p;\n"
var w=Object.prototype.hasOwnProperty
for(var v in z){if(w.call(z,v)){x+="this."+v+"= properties."+v+";\n"}}x+="}\n"
var u=new Function(x)
u.prototype=y
y.constructor=u
u.p=z
return u}}//@ sourceMappingURL=out.js.map
Run Code Online (Sandbox Code Playgroud)

在2013-04-26,缩小的代码是1386个字符.

  • 心灵难以置信.当他们说"转换为JavaScript"时,它们是否意味着"包含用JavaScript编写的Dart运行时程序"?! (12认同)
  • Dart最终会比JS更好/更小.但它还不存在.在JS中,如果你想使用像jQuery这样的库,你需要包含整个库,即使你只使用它中的一些函数.Dart可以更好地分析使用情况并从您的所有代码中创建一个包,该代码只包含您所使用的所有库所需的内容.即使是最好的JS压缩器也永远不会对您的代码有一定程度的了解,以便能够做好工作.所以请耐心等待.:-) (7认同)
  • 我很确定这是一个很大的膨胀,因为它是一个早期的预览.他们遵循的观点是,以后优化更好.因此,我希望输出的大小会大幅减少,只包括所需的内容.虽然我想知道他们是否需要和包含很多动态代码,比如来自eval()的东西. (6认同)
  • 我刚刚编辑了这个答案,以反映2012-05-12的真实情况 (4认同)
  • "我希望这一切能够为我们所有人成功" - 这真的是*真的*破坏了吗?我们需要从中拯救出来吗?Google*之前提到的*基本缺陷*实际上是*基本的吗?我很难相信它. (2认同)

Flo*_*sch 10

Dart-> JavaScript编译器的输出是移动目标.第一个版本(技术预览版)没有做很多树摇晃,因此非常大.新的(实验性)青蛙编译器在这方面要好得多(David Chandler的博客),但我希望DartC也能大大提高.

  • 作为更新,frog将被dart2js取代,而dartc现在只是一个分析器(不再是编译器) (4认同)