[英]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, and when I want to call something on that returned value my code IntelliSense is not working, can anybody help me with the IntelliSense. 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。 Also sharing the code.
也分享代码。 I am getting the IntelliSense for the getInstance method, but when i wan to use obj.setDebug I am not getting any code suggestion or IntelliSense for that part.
我得到了getInstance方法的 IntelliSense,但是当我想使用obj.setDebug时,我没有得到任何代码建议或 IntelliSense。
That Obj.isDebug autocomplete i am not getting
我没有得到那个 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())
Do getInstance
automatically while defining Log
, it'll be more simple:在定义
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());
If you need more complex function with separate getInstance
method, just follow the logic and feel free to add as much additional layers as you need... Below code is just for example.如果您需要更复杂的 function 和单独的
getInstance
方法,只需按照逻辑并随意添加所需的额外层......下面的代码只是示例。 In given context it is pointless as I've mentioned before:在给定的上下文中,正如我之前提到的,这是毫无意义的:
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.