63

Jak mogę powiedzieć JSDoc o strukturze zwróconego obiektu. Znalazłem składnię @return {{field1: type, field2: type, ...}} description i próbowała go:JSDoc: Zwróć obiekt struktury

/** 
* Returns a coordinate from a given mouse or touch event 
* @param {TouchEvent|MouseEvent|jQuery.Event} e  
*   A valid mouse or touch event or a jQuery event wrapping such an 
*   event. 
* @param {string} [type="page"] 
*   A string representing the type of location that should be 
*   returned. Can be either "page", "client" or "screen". 
* @return {{x: Number, y: Number}} 
*   The location of the event 
*/ 
var getEventLocation = function(e, type) { 
    ... 

    return {x: xLocation, y: yLocation}; 
} 

Chociaż analizuje powodzeniem otrzymaną dokumentację po prostu stwierdza:

Returns: 
    The location of an event 
    Type: Object 

Zajmuję API i trzeba, aby ludzie wiedzieli o obiekcie że zostanie zwrócony. Czy to możliwe w JSDoc? Używam JSDoc3.3.0-beta1.

Odpowiedz

106

zdefiniować oddzielnie:

/** 
* @typedef {Object} Point 
* @property {number} x The X Coordinate 
* @property {number} y The Y Coordinate 
*/ 

i zastosowanie:

/** 
* Returns a coordinate from a given mouse or touch event 
* @param {TouchEvent|MouseEvent|jQuery.Event} e  
*   A valid mouse or touch event or a jQuery event wrapping such an 
*   event. 
* @param {string} [type="page"] 
*   A string representing the type of location that should be 
*   returned. Can be either "page", "client" or "screen". 
* @return {Point} 
*   The location of the event 
*/ 
var getEventLocation = function(e, type) { 
    ... 

    return {x: xLocation, y: yLocation}; 
} 
+2

Dzięki. Wiele instrukcji '@ return' rzeczywiście działa, ale są one wypisane na wyjściu tak, jakby były kilkoma zwrotami (jeden punkt wypunktowania określa" point - Object ", a następnie dwa inne punkty dla' point.x - Number' i 'point .y - Number'). Chociaż mogę z tym żyć, przypuszczam, że nie ma sposobu na skondensowane wyjście zwróconego obiektu? A może masz przynajmniej wpisy z wcięciem 'point.x' i' point.y'? – BlackWolf

+0

próbował już drugi przykład? – BGerrissen

+0

Tak, to wydaje się najlepszą opcją. Pomyślałem, że może istnieć sposób na posiadanie nienazwanego obiektu powrotu, ale podejście '@ typedef' jest najbardziej przejrzyste pod względem wydajności dokumentacji, dzięki! – BlackWolf

10

Czysty rozwiązaniem jest napisanie klasy i powrotu to.

+0

Kiedy robię to, ale korzystam z Kompilatora zamykania Google, pojawia się ostrzeżenie: "nie mogę utworzyć wystąpienia innego niż konstruktor". Próbowałem dodać @constructor do funkcji (powyżej @return), ale to nie pomogło. Jeśli ktoś wie, jak to naprawić, proszę dać mi znać. Dzięki! – AndroidDev