I'm trying to document the following code but I seem to be unable to make JSDoc document that class or even reference it's existence.
// SomeMixin.js
export default superclass => class SomeMixin extends superclass {
// class code goes here
func() {}
};
import SomeMixin from './SomeMixin';
import { EventEmitter } from 'events';
/**
* @param {SomeMixin} bar
*/
function foo(bar) {
}
class MyClass extends SomeMixin(EventEmitter) {}
const item = new MyClass();
foo(bar);
How can I document this code so that JSDoc sees that SomeMixin
can be inherited (is a class) and that when I make an instance of MyClass
that it inherits the method func
.
The call() allows for a function/method belonging to one object to be assigned and called for a different object. call() provides a new value of this to the function/method. With call() , you can write a method once and then inherit it in another object, without having to rewrite the method for the new object.
There are two types of Class in ES6: parent class/super class: The class extended to create new class are know as a parent class or super class. child/sub classes: The class are newly created are known as child or sub class. Sub class inherit all the properties from parent class except constructor.
Classes are a template for creating objects. They encapsulate data with code to work on that data. Classes in JS are built on prototypes but also have some syntax and semantics that are not shared with ES5 class-like semantics.
An instance is an object containing data and behavior described by the class. The new operator instantiates the class in JavaScript: instance = new Class() . const myUser = new User(); new User() creates an instance of the User class.
With the help of https://stackoverflow.com/a/52335792/5458200 I got:
// SomeMixin.js
/**
* @template T
* @typedef {new(...args: any[]) => T} Constructor
**/
/**
* @template {Constructor<{}>} T
* @param {T} superclass
*/
function mixin(superclass) {
return class SomeMixin extends superclass {
// class code goes here
func() {}
};
}
export default mixin
// index.js
import createMixin from "./SomeMixin";
import { EventEmitter } from "events";
/**
*
* @param {MyClass} bar
*/
function foo(bar) {
bar;
}
class MyClass extends createMixin(EventEmitter) {}
const item = new MyClass();
foo(item);
Here is a version that I tried. Not the best solution but in VSCode I can see func
and methods from EventEmitter
.
// SomeMixin.js
import { EventEmitter } from "events";
/**
* @param {EventEmitter} superclass
*/
function mixin(superclass) {
/**
* @extends EventEmitter
*/
class SomeMixin extends superclass {
// class code goes here
func() {}
}
return SomeMixin;
}
export default mixin;
// index.js
import SomeMixin from "./SomeMixin";
import { EventEmitter } from "events";
class MyClass extends SomeMixin(EventEmitter) {}
const item = new MyClass();
But still, need to figure out how to work with Generic Types. Probably this question may help.
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