本文介绍了这个生成的代码应该(打算)做什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在自动生成的javascript文件中看到了这一点:

I saw this in an auto-generated javascript file:

function map(x){
    x={x:x};
    delete x.x;
    return x
}

我的结论是,它是用来创建对象的,但是为什么要以这种方式创建对象?是图案吗?

My conclusion is that is used to create an object, but why create it in that way? Is it a pattern?

更新

更多信息,创建此代码的工具是Google的dart2js,该代码用于此上下文:

More info, the tool that created this code is dart2js from Google, the code is used in this context:

(function (reflectionData) {
  function map(x){x={x:x};delete x.x;return x}
  if (!init.libraries) init.libraries = [];
  if (!init.mangledNames) init.mangledNames = map();
  if (!init.mangledGlobalNames) init.mangledGlobalNames = map();
  if (!init.statics) init.statics = map();
  if (!init.interfaces) init.interfaces = map();

推荐答案

在dart源代码中,有一条注释指出该技术是出于v8性能原因而使用的:

In the dart source, there's a comment which says that this technique is used for v8 performance reasons:

// [map] returns an object literal that V8 shouldn't try to optimize with a
// hidden class. This prevents a potential performance problem where V8 tries
// to build a hidden class for an object used as a hashMap.

https ://github.com/dart-lang/bleeding_edge/blob/4dde22bc006605fc168cefcc0807c43354463b6e/dart/sdk/lib/_internal/compiler/implementation/js_emitter/reflection_data_parser.dart#L17-L19

此处的map一词是指关联数组

这篇关于这个生成的代码应该(打算)做什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 03:08