Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Documenting classes that are generated through functions (mixing)

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.

like image 213
Mechazawa Avatar asked Feb 19 '20 14:02

Mechazawa


People also ask

How do you call a class function in JavaScript?

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.

What are classes in ES6?

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.

What are classes in JavaScript?

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.

How do you instantiate a class in JavaScript?

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.


1 Answers

Update

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);

solution 2


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();

solution 1

But still, need to figure out how to work with Generic Types. Probably this question may help.

like image 200
strdr4605 Avatar answered Sep 17 '22 18:09

strdr4605