如何创建具有默认功能和持久属性的javascript函数构造函数?

如何创建具有默认功能和持久属性的javascript函数构造函数?,javascript,prototype,Javascript,Prototype,我有一个稍微不寻常的模式,我正在努力实现,但还没有完全弄明白。我的目标是创建一个名为debugLog的函数,作为灵活的console.log替换,可以按如下方式调用: debugLog('thing to log #1', 'thing to log #2', objectToLog1, objectToLog2); ^^参数的数量应该是任意的,就像使用console.log一样 这就是我所说的“默认”功能。现在我还想通过属性函数添加一些附加功能 示例: debugLog.setDebugFl

我有一个稍微不寻常的模式,我正在努力实现,但还没有完全弄明白。我的目标是创建一个名为debugLog的函数,作为灵活的console.log替换,可以按如下方式调用:

debugLog('thing to log #1', 'thing to log #2', objectToLog1, objectToLog2);
^^参数的数量应该是任意的,就像使用console.log一样

这就是我所说的“默认”功能。现在我还想通过属性函数添加一些附加功能

示例:

debugLog.setDebugFlag(true); // sets the internal this.debugFlag property to true or false depending on param
我试图在Node中实现这一点,但到目前为止,我还没有完全实现这一模式:

var debugLog = function () {
  this.debugFlag = this.debugFlag || true;
  if (this.debugFlag) {
    console.log.apply(null, arguments);
  } else {
    // production mode, nothing to log
  }
};

debugLog.prototype.setDebugFlag = function (flagBool) {
  this.debugFlag = flagBool;
}

module.exports = new debugLog();
此模块将包含在使用标准require模式的节点应用程序中:

var debugLog = require('./debugLog.js');
问题是如何实现具有默认功能的函数对象的这种模式,以及扩展的“属性”样式的函数?请注意,我已经知道所有功能都来自函数属性(如debugLog.log()和debugLog.setDebugFlag())的典型模式。但是,这种模式并没有实现我的主要目标,即简化默认功能,直接调用该功能


甚至可以这样做吗?

你可以这样做

var debugLog = (function() {
  var debugFlag = true;

  function log() {
    if (debugFlag) {
      console.log.apply(null, arguments);
    } else {
      // production mode, nothing to log
    }
  };

  log.setDebugFlag = function(flag) {
    debugFlag = flag;
  }
  return log;
})();

module.exports = debugLog;

你可以这样做

var debugLog = (function() {
  var debugFlag = true;

  function log() {
    if (debugFlag) {
      console.log.apply(null, arguments);
    } else {
      // production mode, nothing to log
    }
  };

  log.setDebugFlag = function(flag) {
    debugFlag = flag;
  }
  return log;
})();

module.exports = debugLog;

你可以这样做

var debugLog = (function() {
  var debugFlag = true;

  function log() {
    if (debugFlag) {
      console.log.apply(null, arguments);
    } else {
      // production mode, nothing to log
    }
  };

  log.setDebugFlag = function(flag) {
    debugFlag = flag;
  }
  return log;
})();

module.exports = debugLog;

你可以这样做

var debugLog = (function() {
  var debugFlag = true;

  function log() {
    if (debugFlag) {
      console.log.apply(null, arguments);
    } else {
      // production mode, nothing to log
    }
  };

  log.setDebugFlag = function(flag) {
    debugFlag = flag;
  }
  return log;
})();

module.exports = debugLog;

您可以使用闭包,如下所示:

// debugLog.js
var debugFlag = true;

function debugLog() {
    if (debugFlag) {
        console.log.apply(null, arguments);
    } else {
        // production mode, nothing to log
    }
}

debugLog.setDebugFlag = function (newFlag) {
    debugFlag = newFlag;
}

module.exports = debugLog;
然后像这样使用它:

// otherFile.js

var debugLog = require('./debugLog');
debugLog('Hey!');
debugLog.setDebugFlag(false);
debugLog('This wont appear!');

您可以使用闭包,如下所示:

// debugLog.js
var debugFlag = true;

function debugLog() {
    if (debugFlag) {
        console.log.apply(null, arguments);
    } else {
        // production mode, nothing to log
    }
}

debugLog.setDebugFlag = function (newFlag) {
    debugFlag = newFlag;
}

module.exports = debugLog;
然后像这样使用它:

// otherFile.js

var debugLog = require('./debugLog');
debugLog('Hey!');
debugLog.setDebugFlag(false);
debugLog('This wont appear!');

您可以使用闭包,如下所示:

// debugLog.js
var debugFlag = true;

function debugLog() {
    if (debugFlag) {
        console.log.apply(null, arguments);
    } else {
        // production mode, nothing to log
    }
}

debugLog.setDebugFlag = function (newFlag) {
    debugFlag = newFlag;
}

module.exports = debugLog;
然后像这样使用它:

// otherFile.js

var debugLog = require('./debugLog');
debugLog('Hey!');
debugLog.setDebugFlag(false);
debugLog('This wont appear!');

您可以使用闭包,如下所示:

// debugLog.js
var debugFlag = true;

function debugLog() {
    if (debugFlag) {
        console.log.apply(null, arguments);
    } else {
        // production mode, nothing to log
    }
}

debugLog.setDebugFlag = function (newFlag) {
    debugFlag = newFlag;
}

module.exports = debugLog;
然后像这样使用它:

// otherFile.js

var debugLog = require('./debugLog');
debugLog('Hey!');
debugLog.setDebugFlag(false);
debugLog('This wont appear!');


OP正在谈论一个NodeJS模块。。这对clientsideOP来说是个好消息,因为它正在谈论一个NodeJS模块。。这对clientsideOP来说是个好消息,因为它正在谈论一个NodeJS模块。。这对clientsideOP来说是个好消息,因为它正在谈论一个NodeJS模块。。这对客户有好处谢谢!就是这样!如果该模块中有更多函数,则缺少封装可能会导致问题。@SpiderPig它被完美封装,可以在NodeJS中用作模块。。比将其封装在不必要的iLife中要好得多,iLife还可以捕获任何“该模块中的更多函数”的作用域。@AlexMcMillan我理解您关于将其封装为节点中的模块的评论,因为该文件将自动拥有自己的作用域,从而防止泄漏。但是,IIFE还能捕获“该模块中的更多函数”的作用域是什么意思?@JoJo有一个google for“javascript闭包”—IIFE的目的是为所使用的变量创建一个新的作用域。这个“新”作用域围绕着周围的作用域关闭,这意味着唯一的区别是
debugFlag
在我的示例中可用于模块中的其他函数,但在IIFE示例中不可用(除非这些函数也在IIFE中声明)。这既有优点也有缺点,在这两种情况下都可以很容易地纠正。。但是你很少看到整个NodeJS模块被封装在IILife中,因为它毫无意义。谢谢!就是这样!如果该模块中有更多函数,则缺少封装可能会导致问题。@SpiderPig它被完美封装,可以在NodeJS中用作模块。。比将其封装在不必要的iLife中要好得多,iLife还可以捕获任何“该模块中的更多函数”的作用域。@AlexMcMillan我理解您关于将其封装为节点中的模块的评论,因为该文件将自动拥有自己的作用域,从而防止泄漏。但是,IIFE还能捕获“该模块中的更多函数”的作用域是什么意思?@JoJo有一个google for“javascript闭包”—IIFE的目的是为所使用的变量创建一个新的作用域。这个“新”作用域围绕着周围的作用域关闭,这意味着唯一的区别是
debugFlag
在我的示例中可用于模块中的其他函数,但在IIFE示例中不可用(除非这些函数也在IIFE中声明)。这既有优点也有缺点,在这两种情况下都可以很容易地纠正。。但是你很少看到整个NodeJS模块被封装在IILife中,因为它毫无意义。谢谢!就是这样!如果该模块中有更多函数,则缺少封装可能会导致问题。@SpiderPig它被完美封装,可以在NodeJS中用作模块。。比将其封装在不必要的iLife中要好得多,iLife还可以捕获任何“该模块中的更多函数”的作用域。@AlexMcMillan我理解您关于将其封装为节点中的模块的评论,因为该文件将自动拥有自己的作用域,从而防止泄漏。但是,IIFE还能捕获“该模块中的更多函数”的作用域是什么意思?@JoJo有一个google for“javascript闭包”—IIFE的目的是为所使用的变量创建一个新的作用域。这个“新”作用域围绕着周围的作用域关闭,这意味着唯一的区别是
debugFlag
在我的示例中可用于模块中的其他函数,但在IIFE示例中不可用(除非这些函数也在IIFE中声明)。这既有优点也有缺点,在这两种情况下都可以很容易地纠正。。但是你很少看到整个NodeJS模块被封装在IILife中,因为它毫无意义。谢谢!就是这样!如果该模块中有更多函数,则缺少封装可能会导致问题。@SpiderPig它被完美封装,可以在NodeJS中用作模块。。比将其封装在不必要的iLife中要好得多,iLife还可以捕获任何“该模块中的更多函数”的作用域。@AlexMcMillan我理解您关于将其封装为节点中的模块的评论,因为该文件将自动拥有自己的作用域,从而防止泄漏。但是你说iLife还可以捕获“该模块中的更多函数”的范围是什么意思?@JoJo有一个google for“javascr”