确保功能范围

时间:2018-08-15 03:41:40

标签: javascript function scope

我写了一些正在运行的代码,我的下一个战斗是重构,我认为在范围方面我做错了什么。我正在检索消息数据,并试图对其进行修改:

function handleMessage(message) {
if(message.includes('dns')) {
    // Take the data I want and re-organize for the API to use
    // This removes the user ID text <@XXXXXXXXX>
    var buf1 = Buffer.allocUnsafe(26);
    buf1 = message;
    buf2 = buf1.slice(13, buf1.length);
    message = buf2.toString('ascii', 0, buf2.length);

    /* There may be a better way with the Slack API, but this will work
    returns URL as a string without extra formatting. Pre-formatted text appears like:
    <http://webbhost.net|webbhost.net> */
    var n = message.indexOf('|');
    var o = message.indexOf('>');
    var n = n+1;
    var o = o-2;
    var s1 = message.substr(n, o);
    var p = s1.indexOf('>');
    var s2 = s1.substr(0, p);
    message = s2;
    dnsLookup(message);

} else if(message.includes(' whois')) {
    // Take the data I want and re-organize for the API to use
    // This should probably be it's own function
    var buf1 = Buffer.allocUnsafe(26);
    buf1 = message;
    buf2 = buf1.slice(13, buf1.length);
    message = buf2.toString('ascii', 0, buf2.length);

    var n = message.indexOf('|');
    var o = message.indexOf('>');
    var n = n+1;
    var o = o-2;
    var s1 = message.substr(n, o);
    var p = s1.indexOf('>');
    var s2 = s1.substr(0, p);
    message = s2;
    whoisLookup(message);
}

很显然,我在这里重复了很多代码,我想从中得出2个函数。现在,我尝试在此函数中执行此操作,当我将它们设置为不永久更新要发送的变量的函数时(当我检查第一个函数的结果以及开始时其次,看起来就像在运行第一个函数之前一样。

我还有其他需要看的东西吗?我可以将其作为一个功能来完成,但是我正尝试在将来可能遇到的其他情况下适应未来。

这是我更改后的样子:

function handleMessage(message) {
function removeID(message) {
    var buf1 = Buffer.allocUnsafe(26);
    buf1 = message;
    buf2 = buf1.slice(13, buf1.length);
    message = buf2.toString('ascii', 0, buf2.length);

    console.log(message);
    }
function removeSlackURL(message){
    console.log("test");
    console.log(message);
    var n = message.indexOf('|');
    var o = message.indexOf('>');
    var n = n+1;
    var o = o-2;
    var s1 = message.substr(n, o);
    var p = s1.indexOf('>');
    var s2 = s1.substr(0, p);
    message = s2;

}
if(message.includes('dns')) {
    // Take the data I want and re-organize for the API to use
    // This removes the user ID text <@XXXXXXXXX>


    removeID(message);
    removeSlackURL(message);

2 个答案:

答案 0 :(得分:1)

无论何时将message传递给函数,都将对字符串的引用传递给一个单独的变量,该变量的作用域仅限于该函数。如果您在函数内的message变量中分配了不同的字符串,则它实际上将对该引用的引用重新分配给新值,而不会以任何方式影响原始message。 它等效于:

var a = 2;
var b = a; //b = 2
b = 3;
console.log(a); //2

这里的a是您原始的message,它被传递给函数内部类似于message的变量b

保留更改的最佳方法是从所有函数返回最终修改后的字符串,并使用返回的值替换初始值。

function handleMessage(message){
  function removeID(message){
     ....
     ....
     return buf2.toString('ascii', 0, buf2.length);
  }

  function removeSlackURL(message){
     console.log("test");
     console.log(message);
     var n = message.indexOf('|');
     var o = message.indexOf('>');
     var n = n+1;
     var o = o-2;
     var s1 = message.substr(n, o);
     var p = s1.indexOf('>');
     var s2 = s1.substr(0, p);
     message = s2;
     return message;
  }

  if(message.includes('dns')) {
      // Take the data I want and re-organize for the API to use
      // This removes the user ID text <@XXXXXXXXX>


      message = removeID(message);
      message = removeSlackURL(message);

  ....
  ....
  return message;
}

答案 1 :(得分:0)

变量在javascript中具有功能范围。消息已经是外部函数的函数变量,外部函数内部的函数不必在其中包含消息变量。

内部函数可以访问外部函数的变量。

function handleMessage(message) {
  function removeID() {
    var buf1 = Buffer.allocUnsafe(26);
    buf1 = message;
    buf2 = buf1.slice(13, buf1.length);
    message = buf2.toString('ascii', 0, buf2.length);

    console.log(message);
  }
  function removeSlackURL(){
    console.log("test");
    console.log(message);
    var n = message.indexOf('|');
    var o = message.indexOf('>');
    var n = n+1;
    var o = o-2;
    var s1 = message.substr(n, o);
    var p = s1.indexOf('>');
    var s2 = s1.substr(0, p);
    message = s2;

  }
  // doing this on either cases
  removeID();
  removeSlackURL();
  if(message.includes('dns')) {
    // Take the data I want and re-organize for the API to use
    // This removes the user ID text <@XXXXXXXXX>
  } else if (message.includes()) {

  }
}