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

简单的转换是什么样子的?

推荐答案

main() {
      print('Hello, Dart!');
}

使用dart2js (as of 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

Note for posterity:.这个问题的原始答案已作了修改,以反映当前的情况.

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

2013-04-26,缩写代码为1386个字符.

Dart相关问答推荐

聆听Firestore计数()

如何在流侦听器中对异步函数调用进行单元测试

在Dart中进行系统调用?

Dart:使用正则表达式从字符串中删除空格

在 Flutter 中检测键盘事件

Swipe返回手势

如何使用Flutter在按钮网格中滑动(swipe)/拖动(drag) 2 个或更多按钮

如何go 除字符串中的变音符号?

Angular 2,使用 body 作为根 Select 器,而不是 my-app

在 Dart 中向服务器发出多个独立请求的最佳方式

判断Dart中是否提供了可选参数

如何在 Flutter 中使用 Provider 显示来自 ChangeNotifier 的错误

是否有必要为Flatter安装Android Studio,或者可以用什么替代Android Studio?

有没有更好的方法来找出文件或目录是否存在?

如何从列表中的元素创建逗号分隔的字符串

如何对map值进行排序?

Flutter/Dart:按第一次出现拆分字符串

如何在 base64 中编码 Dart 字符串?

从函数返回多个值

如何在 Dart 中生成随机数?