How to extend JS class in ReasonML

佐手、 提交于 2019-12-23 12:28:52

问题


For example, I have an es6-like class:

class Foo {...}

And I want to extend it:

class Bar extends Foo {...}

In reason-react documentation I found examples, but I'm not sure that it's appropriate for me:

let component = ReasonReact.reducerComponent "TodoAppRe";
let make _children => {
  ...component,
  initialState: fun () => {count: 0},
  <...>

But when i try to write code in this style, i get an error:

let myclass unit => {
  ...mysuperclass,
    hello: fun () => {
      Js.log "FooBar";
    }
};

Error: Unbound record field update

(In this example mysuperclass is external from 3rd-party js-library).

Maybe I am doing something wrong?


回答1:


let foo bar => { ...baz, quux: ... } is not a syntax for inheritance, it's a syntax for composition. Specifically, it's taking the baz record value (not object) and updating its quux member (not method).

If you want to extend a JS class in Reason/BuckleScript, first keep in mind that BuckleScript generates code that's backwards-compatible with ES5, so that you don't need to use Babel. In fact BuckleScript doesn't directly support extending a class.

But, you can include raw JavaScript in your Reason source code using BuckleScript's [%%bs.raw] attribute ( https://bucklescript.github.io/bucklescript/Manual.html#_embedding_raw_js_code_as_statements ):

/* MyProject_Animal.re */

[%%bs.raw {|

// Dummy class for example:
class Animal {
  speak() {}
}

// or:
// import Animal from "somewhere";

class Dog extends Animal {
  _name;

  constructor(name) {
    super();
    this._name = name;
  }

  speak() {
    console.log(`${this._name} says woof!!`);
    super.speak();
  }
}
|}];

And then you can write a binding to the Dog class in the code below that:

module Animal_Dog = {
  type t;

  [@bs.new] external make: string => t = "Dog";
  /* Need this to ensure correct usage in other modules */
  let make = make;

  [@bs.send] external speak: t => unit = "";
};

let spot = Animal_Dog.make("Spot");
Animal_Dog.speak(spot);

But, remember that since this ES2015, you will need to run it through Babel if you want it to be backwards-compatible with pre-ES2015 browsers.



来源:https://stackoverflow.com/questions/46454098/how-to-extend-js-class-in-reasonml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!