I am embedding SpiderMonkey to make my C++ library scriptable. To make it extendable, I need it possible to define a new class (JavaScript) inheriting one C++ base class of the library.
Is there an example showing me how to do that using SpiderMonkey?
By calling the super() method in the constructor method, we call the parent's constructor method and gets access to the parent's properties and methods. Inheritance is useful for code reusability: reuse properties and methods of an existing class when you create a new class.
Inheriting "methods" In JavaScript, any function can be added to an object in the form of a property. An inherited function acts just as any other property, including property shadowing as shown above (in this case, a form of method overriding).
JavaScript supports inheritance if you use it. Look up "JavaScript prototype chain". As for "Why javascript does not support classical inheritance as a default option?" - because that's how JavaScript was defined.
Inheritance enables you to create new classes that reuse, extend, and modify the behavior defined in other classes. The class whose members are inherited is called the base class, and the class that inherits those members is called the derived class.
I'd think you cannot subclass a C++ class in SpiderMonkey/JavaScript and use it transparently in the rest of your C++ code. However, you could create a wrapping class in C++ that delegates its work to a couple of JavaScript classes/functions. If you make your C++ class configurable enough that should work for your situation. Both calling JavaScript code from C and C code from JavaScript is explaind in the JSAPI User Guide page (I think you've already read that one).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With