簡體   English   中英

模塊名稱是在綁定中使用的陰影全局名稱

[英]module name is shadowing global name used in binding

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()

首先,我認為這是 BuckleScript 中的一個錯誤。 這個問題最近在問題 #3268 中提出並在此提交中部分解決,但它仍然留下了很多未保留的名稱。 您應該考慮在那里或在新問題中提出這一點。

同時,您可以通過完全限定名稱來解決此問題。 Intl實際上不是一個全局對象,而是附着全局對象,這在網頁的背景是window 但是由於 JavaScript 會在本地環境中找不到時在全局對象上查找名稱,因此它看起來非常像一個全局名稱。

因此,如果您將make更改為:

[@bs.new] external make: unit => Impl.t = "window.Intl.DateTimeFormat";

它應該可以正常工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM