How to properly extends a class? #1202
-
| I've already setup a class  
 But then a problem comes out. The result object will still contains the classId of  In perspective of js, I think I shall always use  E.g. I've got an object of  | 
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
| There's no concept of inheritance at the C API level but you can distinguish between types in your C callback: JSValue callback(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv) {
    Inherit *inherit;
    Base *base;
    if ((inherit = JS_GetOpaque(this_val, inherit_classid))) {
        // called as Inherit method
    } else if ((base = JS_GetOpaque(this_val, base_classid))) { // JS_GetOpaque2 if it should throw TypeError
        // called as Base method
    } else {
        // called as something else, maybe throw a TypeError
    }
   // ...
}As to constructor inheritance, just call the constructor function manually: JSValue base_constructor(JSContext *ctx, JSValueConst new_target, int argc, JSValueConst *argv) {
    // ...
    return JS_UNDEFINED;
}
JSValue inherit_constructor(JSContext *ctx, JSValueConst new_target, int argc, JSValueConst *argv) {
    // ...
    if (JS_IsException(base_constructor(ctx, new_target, argc, argv)))
        return JS_EXCEPTION;
    // ...
    return JS_UNDEFINED;
}Your base constructor needs to be aware it can get called that way, of course; it has to take that into account when e.g. calling JS_GetOpaque/JS_SetOpaque. You could use the JSCFunctionMagic function prototype to signal that. JSCFunctionMagic is the same as regular JSCFunction except it takes an extra  | 
Beta Was this translation helpful? Give feedback.
There's no concept of inheritance at the C API level but you can distinguish between types in your C callback:
As to constructor inheritance, just call the constructor function manually: