我有一个特殊的 JS 单例原型(prototype)函数。
模式看起来或多或少类似于下面的示例。工作正常并完成工作,但遗憾的是 PhpStorm 对于自动完成和其他有用的东西完全盲目。
如何使用 JSDoc 告诉 IDE new Item 将导致使用 ItemPrototype 构建新对象,以便 new Item(1).getId() 将指向代码中的正确位置?
预先感谢您的宝贵时间。
var Item = (function(){
var singletonCollection = {};
var ItemPrototype = function(id){
this.getId = function() {
return id;
};
return this;
};
var Constructor = function(id){
if (! (id in singletonCollection)) {
singletonCollection[id] = new ItemPrototype(id);
}
return singletonCollection[id];
};
return Constructor;
})();
最佳答案
您可以尝试以下操作:
/**
* A description here
* @class
* @name Item
*/
var Item = (function(){
var singletonCollection = {};
var ItemPrototype = function(id){
/**
* method description
* @name Item#getId
*/
this.getId = function() {
return id;
};
return this;
};
var Constructor = function(id){
if (! (id in singletonCollection)) {
singletonCollection[id] = new ItemPrototype(id);
}
return singletonCollection[id];
};
return Constructor;
})();
关于javascript - 特殊单例模式的 JSDoc,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33416937/