How To Extend Js Class In Reasonml
Solution 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:classAnimal {
speak() {}
}
// or:// import Animal from "somewhere";classDogextendsAnimal {
_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:
moduleAnimal_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.
Post a Comment for "How To Extend Js Class In Reasonml"