模块的ES2015初始化不起作用



我尝试构建包装集合的模块失败

我有这样的东西:

// topic: chess gaming
// file: src/core/Refs.js
const COLS = 'abcdefgh'.split('')
const ROWS = '12345678'.split('')
const ALL = new Map()
class Ref {
constructor (col, row) {
this.col = col
this.row = row
this.key = String(col + row)
}
// translate to ref to obtain another
// or null, if off-board
//
// ref.translate(0, 0) === ref (ok ?)
//
translate (dcol, drow) {
// compute something for dcol and drow
// ...
return ALL.get( COLS[colIdx] + ROWS[rowIdx] )
}
}
// the code which seems to not work propertly
for(let c of COLS) {
for(let r of ROWS) {
ALL.set(String(c + r), new Ref(c, r))
}
}
export default {
COLS, ROWS, ALL,
has (ref) {
return ALL.has(ref)
},
get (ref) {
return ALL.get(ref)
},
// trying to grant/warrant "immutability"
// for the "ALL" collection inside the module
keys() {
return [...ALL.keys()]
}
}

在我用正确的标志(危险ForOf,..(和对象分配来遵守Buble模块之后

然后我用业力+茉莉花测试,第一个测试:

it ('should be 64-length array', function () {
expect (Refs.keys().length).toEqual(64)
})

将以"期望 1 等于 64"为目标,并且 Refs.ALL 的日志似乎显示一个空地图 althougth Refs.COLS 和 Refs.ROWS 已正确初始化。

发生了什么以及如何解决它?

编辑:

@Bergi:当然,公开 Refs.ALL 会破坏不可变性,而是出于调试目的

我不完全确定如何捕获测试包输出,但是查看gulp + rollup开发包,方法键((行:

return [...ALL.keys()]

替换为:

return [].concat(ALL.keys())

生成一个包含 MapIterator 的 1 元素数组,这打破了测试。 放这样的东西:

return Array.from( ALL.keys() )

将解决问题,但有可能无法在旧版浏览器中正常工作!

我已经在我的存储库中推送了代码:https://github.com/hefeust/colorchess-v2

希望这可以帮助修复错误:如何将源代码中的传播 (...( 运算符转换为具有正确 Object.assign polyfill 的捆绑包?

Bublé 不支持迭代器,这就是为什么它将具有扩展语法的数组文字转换为串联的原因。请改用Array.from(无论如何,这更具描述性(。

return Array.from( ALL.keys() )可以解决问题,但有可能无法在旧版浏览器中正常工作!

这应该无关紧要 - 您正在使用Map对象及其返回迭代器的keys()方法,该方法在旧版浏览器中也不起作用。如果您打算支持它们,无论如何都必须使用填充物 - 并且您也会获得Array.from的填充物。

最新更新