Javascript 抽象对象或创建类对象

Javascript 抽象对象或创建类对象,javascript,Javascript,很抱歉让人困惑的标题,我真的不知道如何命名我想做的事情。 我目前有以下代码: const utils = (function () { const tabsWithAudio = { tabsArray: [], add: function (tabId) { const index = this.tabsArray.indexOf(tabId); if (index < 0) { this.tabsArray.push(tab

很抱歉让人困惑的标题,我真的不知道如何命名我想做的事情。 我目前有以下代码:

const utils = (function () {

  const tabsWithAudio = {
    tabsArray: [],
    add: function (tabId) {
      const index = this.tabsArray.indexOf(tabId);
      if (index < 0) {
        this.tabsArray.push(tabId);
      }
    },
    remove: function (tabId) {
      const index = this.tabsArray.indexOf(tabId);
      if (index >= 0) {
        this.tabsArray.splice(index, 1);
      }
    },
    contains: function (tabId) {
      return (this.tabsArray.indexOf(tabId) > -1);
    },
  };


  const tabsWithWidget = {
    tabsArray: [],
    add: function (tabId) {
      const index = this.tabsArray.indexOf(tabId);
      if (index < 0) {
        this.tabsArray.push(tabId);
      }
    },
    remove: function (tabId) {
      const index = this.tabsArray.indexOf(tabId);
      if (index >= 0) {
        this.tabsArray.splice(index, 1);
      }
    },
    contains: function (tabId) {
      return (this.tabsArray.indexOf(tabId) > -1);
    },
  };

  return {
    tabsWithAudio: tabsWithAudio,
    tabsWithWidget: tabsWithWidget,
  };
}());

module.exports = utils;
但是,由于所有这些对象都是相同的,我想创建一个类似类的对象,我可以在运行中实例化它,例如:

let tabsWithAudio = new utils.tabsHelper();
utils.tabsWithAudio.add(123);
我尝试过这个,但原型中的所有内容都没有导出,我想:

const utils = (function () {

  const tabsHelper = function() {};
  tabsHelper.prototype = {
    tabsArray: [],
    add: function (tabId) {
      const index = this.tabsArray.indexOf(tabId);
      if (index < 0) {
        this.tabsArray.push(tabId);
      }
    },
    remove: function (tabId) {
      const index = this.tabsArray.indexOf(tabId);
      if (index >= 0) {
        this.tabsArray.splice(index, 1);
      }
    },
    contains: function (tabId) {
      return (this.tabsArray.indexOf(tabId) > -1);
    },
  };



  return {
    tabsHelper: tabsHelper,
  };
}());

module.exports = utils;
const-utils=(函数(){
const tabsHelper=函数(){};
tabsHelper.prototype={
tabsArray:[],
添加:函数(tabId){
常量索引=this.tabsArray.indexOf(tabId);
如果(指数<0){
this.tabsArray.push(tabId);
}
},
删除:函数(tabId){
常量索引=this.tabsArray.indexOf(tabId);
如果(索引>=0){
此.tabsArray.splice(索引,1);
}
},
包含:函数(tabId){
返回(this.tabsArray.indexOf(tabId)>-1);
},
};
返回{
tabsHelper:tabsHelper,
};
}());
module.exports=utils;
你知道我错过了什么吗?还有,这叫什么


谢谢。

嘿,康威尔,我读了你的问题,我的答案不会是任何“代码”。你很清楚自己的想法,我相信你只是想澄清一下

您知道这些代码片段不是“类”是正确的,至少在经典意义上是这样。您所指的是“模块”。更具体地说,您使用一个IIFE(创建闭包),然后返回一个对象,该对象引用了该IIFE中的函数。这是一个使用闭包的模块

可以找到有关此模式的更多信息。有一件事需要注意。当使用“类”的“框架”时,不会实例化模块,而是返回模块


JavaScript类被实例化,但模式看起来不同。这里是一个资源,这里是一个链接,通常是语言的标题。另外,关键字“new”仅与实例化关联。实例化与类相关联

嘿,康威尔,我读了你的问题,我的回答不会是任何“代码”。你很清楚自己的想法,我相信你只是想澄清一下

您知道这些代码片段不是“类”是正确的,至少在经典意义上是这样。您所指的是“模块”。更具体地说,您使用一个IIFE(创建闭包),然后返回一个对象,该对象引用了该IIFE中的函数。这是一个使用闭包的模块

可以找到有关此模式的更多信息。有一件事需要注意。当使用“类”的“框架”时,不会实例化模块,而是返回模块

JavaScript类被实例化,但模式看起来不同。这里是一个资源,这里是一个链接,通常是语言的标题。另外,关键字“new”仅与实例化关联。实例化与类相关联

const utils = (function () {

  const tabsHelper = function() {};
  tabsHelper.prototype = {
    tabsArray: [],
    add: function (tabId) {
      const index = this.tabsArray.indexOf(tabId);
      if (index < 0) {
        this.tabsArray.push(tabId);
      }
    },
    remove: function (tabId) {
      const index = this.tabsArray.indexOf(tabId);
      if (index >= 0) {
        this.tabsArray.splice(index, 1);
      }
    },
    contains: function (tabId) {
      return (this.tabsArray.indexOf(tabId) > -1);
    },
  };



  return {
    tabsHelper: tabsHelper,
  };
}());

module.exports = utils;