简体   繁体   English

Javascript function 返回 object 其中包含 3 个 ZC1C425268E68385D1AB5074C17A94

[英]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());

Example is here例子在这里

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());

Example is here例子在这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM