我希望从Publisher生成名为ReasonML的javascript函数,以便我可以在其他文件中使用它,例如:
const publisher = new Publisher("Prasad", "email@email.com", "team@email.com", "rill")
const req = Publisher.toAPI(publisher) // returns {name: "Prasad", email: "email@email.com", team: "team@email.com", service: "rill"}为了实现上述功能,我在名为ReasonML的文件中编写了Util.re代码,即:
type publisher = {
name: string,
emailID: string,
teamEmailID: string,
serviceName: string,
};
type publisherReqBody = {
name: string,
email: string,
team: string,
publisher: string,
};
module Publisher = {
let toAPI = (p: publisher) => {
name: p.name,
email: p.emailID,
team: p.teamEmailID,
publisher: p.serviceName,
};
[@bs.new] external create: unit => publisher = "Publisher";
};在使用ReasonML从JavaScript编译到BuckleScript之后,我得到了什么
// Generated by BUCKLESCRIPT, PLEASE EDIT WITH CARE
function toAPI(p) {
return {
name: p.name,
email: p.emailID,
team: p.teamEmailID,
publisher: p.serviceName
};
}
var Publisher = {
toAPI: toAPI
};
export {
Publisher ,
}我不知道为什么[@bs.new] external create: unit => publisher = "Publisher";线不能工作。我试了一个小时,但没有用。
我的问题:
如何实现我在JavaScript中的第一个片段中提到的功能,它是从ReasonML编译的
非常感谢!
发布于 2020-01-02 11:47:03
外部定义是一个description,用于说明如何使用JavaScript值,而不是单独使用一个值。当您使用该外部,它将内联正确的代码到位.
如果在代码段下面执行let publisher = Publisher.create();,您将看到在调用站点生成的代码如下:
var publisher = new Publisher();https://stackoverflow.com/questions/59562044
复制相似问题