本文介绍了dart:js和js包之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 无论在Dart文档中,建议使用 js 包来实现JavaScript互操作性。 最近发现在SDK中存在 dart:js 包似乎有类似(但不是相同的)接口。 这些包之间有什么区别吗?它们具有等同性吗?推荐哪一个?解决方案 Js interop以 package:js 。它是使用 window.postMessage 构建的。 p> 之后 dart:js 已添加,以提供更好的性能和减少编译的js文件的大小。基本上目标是: 移除作用域和生命周期手动处理 避免noSuchMethod以保持编译大小 一旦 dart:js 已准备就绪, 已被重写以使用 $ d $ b package:js 提供了一个更简单的Api,以增加js大小为代价(因为包:js 使用和 noSuchMethod )。 下面是使用 package:js > dart:js : import'package:js / js.dart' as js; main(){ var pixi = new js.Proxy(js.context.PIXI.Stage,0xffffff); var renderer = js.context.PIXI.autoDetectRenderer(400,400); document.body.append(renderer.view); } import'dart:js'as js; main(){ var pixi = new js.JsObject(js.context ['PIXI'] ['Stage'],[0xffffff]); var renderer = js.context ['PIXI']。callMethod('autoDetectRenderer',[400,400]); document.body.append(renderer ['view']); } Everywhere in Dart documentation it is recommended to use js package for javascript interoperability.However, I have recently found that dart:js package exists in SDK that seems to have similar (but not same) interface.Are there any differences between these packages? Are they feature equivalent? Which one is recommended? 解决方案 Js interop started with package:js. It was built with with window.postMessage.Later dart:js has been added to provide better performance and reduce the size of the compiled js file. Basically the goal were :removing scopes and lifecycle manual handlingavoiding noSuchMethod to keep compilation size as low as possiblerenaming objects to make the api more understandableOnce dart:js has been ready, package:js has been rewrite to use dart:js under the cover.package:js provides a simpler Api that comes at the cost of an increase of the js size (because package:js uses dart:mirrors and noSuchMethod).Here is the same thing done with package:js and dart:js :import 'package:js/js.dart' as js;main() { var pixi = new js.Proxy(js.context.PIXI.Stage, 0xffffff); var renderer = js.context.PIXI.autoDetectRenderer(400, 400); document.body.append(renderer.view);}import 'dart:js' as js;main() { var pixi = new js.JsObject(js.context['PIXI']['Stage'], [0xffffff]); var renderer = js.context['PIXI'].callMethod('autoDetectRenderer', [400, 400]); document.body.append(renderer['view']);} 这篇关于dart:js和js包之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-11 20:39