首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使用新的js0.6.0包将Dart类映射到JS“类”?

如何使用新的js0.6.0包将Dart类映射到JS“类”?
EN

Stack Overflow用户
提问于 2015-10-16 10:55:45
回答 1查看 518关注 0票数 2

index.html

代码语言:javascript
运行
复制
<!doctype html>
<html>
  <head>
  </head>
    <script>
      var Apple = function(type) {
        this.type = type;
        this.color = "red";
      };

      Apple.prototype.getInfo = function() {
        return this.color + ' ' + this.type + ' apple';
      };
    </script>
  <body>
    <script type="application/dart" src="index.dart"></script>
    <script src="packages/browser/dart.js"></script>
  </body>
</html>

index.dart

代码语言:javascript
运行
复制
import 'dart:js' as js;
import 'dart:html' as dom;
import 'package:js/js.dart';

main() {
  // this works fine
  var apple = new js.JsObject(js.context['Apple'], ['Macintosh']);
  print(apple.callMethod('getInfo', []));
  print(new Apple().getInfo());
}

@Js() // about to being changed to @JS
class Apple {
  external String get type;
  external set type(String type);
  external String get color;
  external set color(String color);
  external factory Apple(String type);
}

只是将@Js()注释结果添加到

例外:'dart:js':Failed断言:第393行:'p.isNamed‘不是真。观察http://127.0.0.1:35293/内部错误: Dart_Invoke期望加载库参数“目标”。

Update移除external factory Apple(String type);修复了异常。

现在我得到了

天文台在http://127.0.0.1:38029/收听 红麦金塔苹果 例外:类'Apple‘没有实例方法'getInfo’。 NoSuchMethodError:未找到的方法:'getInfo‘ 接收器:“苹果”的例子 论点:..。 Apple.getInfo main

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-10-16 12:10:12

类需要构造函数delcaration,但不需要factory

有了这个JS

代码语言:javascript
运行
复制
<script>
  var Apple = function(type) {
    this.type = type;
    this.color = "red";
    this.getInfo2 = function() {
      return this.color + ' ' + this.type + ' apple';
    };
  };

  Apple.prototype.getInfo = function() {
    return this.color + ' ' + this.type + ' apple';
  };
</script>

而这个Dart密码

代码语言:javascript
运行
复制
main() {
  var apple = new js.JsObject(js.context['Apple'], ['Macintosh']);
  print(apple.callMethod('getInfo', []));
  print(new Apple('Macintosh').type);
  print(new Apple('Macintosh').getInfo2());
  print(new Apple('Macintosh').getInfo());
}

@Js() // about to being changed to @JS
class Apple {
  external String get type;
  external set type(String type);
  external String get color;
  external set color(String color);
  external String getInfo();
  external String getInfo2();
  external Apple(String type);
}

一切都如愿了。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33168767

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档