[英]Javascript function return object which contains 3 function defination but IntelliSense is not working for that return type
I have written a Logger function in javascript and that is a singleton function, from that function I am returning an object that contains the definition of the two functions, So when I am creating an object of that class I get the returned object contains function definition ,并且当我想在该返回值上调用某些内容时,我的代码 IntelliSense 不起作用,任何人都可以帮助我使用 IntelliSense。 也分享代码。 我得到了getInstance方法的 IntelliSense,但是当我想使用obj.setDebug时,我没有得到任何代码建议或 IntelliSense。
我没有得到那个 Obj.isDebug 自动完成
const Logger = () => {
let instance = null
_isDebug = false
let setDebug = (value = false) => {
_isDebug = value
}
let isDebug = () => {
return _isDebug
}
createInstance = () => ({
setDebug,
isDebug,
log
})
return {
getInstance: () => {
if (!instance) {
instance = createInstance()
}
return instance
}
}
}
const Log = Logger()
const obj = Log.getInstance();
console.log(obj.setDebug())
console.log(obj.isDebug())
在定义Log
时自动执行getInstance
,它会更简单:
const Logger = () => {
// Set app
const app = {
// Set _isDebug option
_isDebug: false,
// Set setDebug method
setDebug: (value = false) => app._isDebug = value,
// Get isDebug method
isDebug: (value = false) => app._isDebug
};
return app;
}
const Log = Logger();
Log.setDebug(true);
console.log(Log.isDebug());
例子在这里
如果您需要更复杂的 function 和单独的getInstance
方法,只需按照逻辑并随意添加所需的额外层......下面的代码只是示例。 在给定的上下文中,正如我之前提到的,这是毫无意义的:
const Logger = () => {
// Set app
const app = {
// Set _isDebug option
_isDebug: false,
// Set isDebug method
setDebug: (value = false) => app._isDebug = value,
// Get isDebug method
isDebug: (value = false) => app._isDebug
};
// Initiator
const initiator = {
// Set init status var
instance: false,
// Set getInstance method
getInstance: () => {
if(!initiator.instance) initiator.instance = app;
return initiator.instance;
}
}
return initiator;
}
const Log = Logger();
const obj = Log.getInstance();
obj.setDebug(true);
console.log(obj.isDebug());
例子在这里
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.