问题描述
tl; dr:如何更改以下绑定以能够编写Intl.DateTimeFormat.make()
而不是Intl_.DateTimeFormat.make()
?
type dateTimeFormat;
[@bs.deriving abstract]
type formatOptions = {
[@bs.optional]
weekday: string,
[@bs.optional]
day: string,
[@bs.optional]
month: string,
};
module Intl_ {
module DateTimeFormat {
module Impl {
type t;
};
[@bs.new] external make: unit => Impl.t = "Intl.DateTimeFormat";
[@bs.send] external format: (Impl.t, Js.Date.t) => string = "";
};
}
Intl_.DateTimeFormat.make()
->Intl_.DateTimeFormat.format(Js.Date.make())
->Js.log;
问题
如果没有下划线,这将编译为:
var Impl = /* module */[];
var DateTimeFormat = /* module */[/* Impl */Impl];
var Intl = /* module */[/* DateTimeFormat */DateTimeFormat];
console.log(new Intl.DateTimeFormat().format(new Date()));
exports.Intl = Intl;
问题是var Intl = ...
new Intl.DateTimeFormat()
了全局Intl
,从而破坏了new Intl.DateTimeFormat()
。
1楼
glennsl
2
已采纳
2019-02-21 11:10:56
首先,我认为这是 BuckleScript 中的一个错误。 这个问题最近在并在部分解决,但它仍然留下了很多未保留的名称。 您应该考虑在那里或在新问题中提出这一点。
同时,您可以通过完全限定名称来解决此问题。
Intl
实际上不是一个全局对象,而是附着在全局对象,这在网页的背景是window
。
但是由于 JavaScript 会在本地环境中找不到时在全局对象上查找名称,因此它看起来非常像一个全局名称。
因此,如果您将make
更改为:
[@bs.new] external make: unit => Impl.t = "window.Intl.DateTimeFormat";
它应该可以正常工作。