我想使用JsDoc來記錄es6類。不能相信你不能將類作爲參數傳遞(類類型,不是實例類型)。JsDoc,ES6和@param {構造函數}
我一直在嘗試的東西,但不能得到這個簡單的代碼工作,使JsDoc不會給我一些警告。
我不能讓它工作,除非我爲每個類創建@typedef,然後手動添加所有自己的和繼承的成員。甚至不能做混音!
有沒有人成功傳遞構造函數/類參數?因此,JsDoc在靜態上下文中,而不是實例上下文?
/**
* @class A
*/
class A {
/**
* @static
*/
static helloFromClassA(){
}
}
/**
* @class B
* @extends A
*/
class B extends A{
/**
* @static
*/
static helloFromClassB(){
}
}
/**
* Class as object
* @param {A} ClassArgument
*/
function fn1(ClassArgument){
ClassArgument.helloFromClassA(); // Unresolved function or method helloFromClassA
// Does not work because ClassArgument is interpreted as an
// instance of A, not A's constructor
}
/**
* // Class as function
* @param {Function} ClassArgument
*/
function fn2(ClassArgument){
ClassArgument.helloFromClassA(); // Unresolved function or method helloFromClassA
// Does not work because ClassArgument is interpreted as an
// empty function, not A's constructor
}
/**
* // Type definition
* @typedef {Object} AClass
* @property {Function} helloFromClassA
* @property {Function} super
*/
/**
* // Trying to mixin the AClass
* @typedef {Object} BClass
* @property {Function} helloFromClassB
* @mixes {AClass}
* @mixes {A}
*/
/**
* // Adding manually all members
* @typedef {Object} BClass2
* @property {Function} helloFromClassB
* @property {Function} helloFromClassA
*/
/**
* @param {BClass} ClassArgument
*/
function fn3(ClassArgument){
ClassArgument.helloFromClassA(); // Unresolved function or method helloFromClassA
// Does not work because the BClass typedef does not take
// into account the mixin from AClass, nor from A
ClassArgument.helloFromClassB(); // No warming
}
/**
* @param {BClass2} ClassArgument
*/
function fn4(ClassArgument){
ClassArgument.helloFromClassA(); // No Warning
ClassArgument.helloFromClassB(); // No warming
// Works because we manually defined the typedef with all own
// and inherited properties. It's a drag.
}
fn1(B);
fn2(B);
fn3(B);
fn4(B);
jsDoc問題:https://github.com/jsdoc3/jsdoc/issues/1088
你聲明的那些方法作爲'靜態' - 在實例上調用它們會起作用,但是對於關鍵字來說這有點奇怪。 – CodingIntrigue
你是什麼意思? ES6有static關鍵字,可以讓你在構造函數而不是原型上聲明方法。這些是**靜態**方法。而且我不是試圖在實例上調用它們,我正在調用構造函數。這裏的問題是JsDoc不允許你聲明一個參數作爲構造函數(一種類型),它只允許你聲明一個參數作爲一個實例。 – Ludo
我明白了。我錯誤地認爲val是一個實例,而不是對函數的引用 – CodingIntrigue