首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >确保函数的作用域

确保函数的作用域
EN

Stack Overflow用户
提问于 2018-08-15 11:41:40
回答 2查看 50关注 0票数 0

我写了一些正在运行的代码,我的下一场战斗是重构,我认为我在作用域方面做得不正确。我正在检索消息数据,并且正在尝试修改它:

代码语言:javascript
复制
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);
}

显然,我在这里重复了很多代码,我想从中创建两个函数。现在我尝试在这个函数中这样做,当我将它们设置为函数时,看起来并没有永久更新我发送的变量(当我检查第一个函数的结果时,在第二个函数的开头,它看起来就像第一个函数运行之前)。

我还需要用其他方式来看待这件事吗?我可以作为一个函数来做这件事,但我正在努力为将来可能遇到的其他场景做好准备。

这是我修改后的样子:

代码语言:javascript
复制
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);
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-08-15 11:50:56

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

代码语言:javascript
复制
var a = 2;
var b = a; //b = 2
b = 3;
console.log(a); //2

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

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

代码语言:javascript
复制
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;
}
票数 2
EN

Stack Overflow用户

发布于 2018-08-15 12:34:12

在javascript中,变量具有函数作用域。message已经是外部函数的函数变量,外部函数内部的函数不需要包含message变量。

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

代码语言: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()) {

  }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51852643

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档