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);
}
显然,我在这里重复了很多代码,我想从中制作 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);
解决方案
每当您将 a 传递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;
}
推荐阅读
- python - ValueError:没有足够的值来解包(预期 2,得到 1)使用列表
- spring-boot - 部署在 PCF 中的 Spring Boot - 响应时间慢。如何优化?
- spring - JPA Repository,指定具体方法的fetch模式
- messaging - Hazelcast - 忽略本地消息
- swift - 如果协议下的类在泛型函数之前是 var,则无法推断泛型参数“T”
- google-chrome-extension - Chrome 扩展程序:单个窗口中每个选项卡上的不同会话
- java - 如果相同的输入来自不同的数据源并且最终对象完全相同,应该使用哪种设计模式
- javascript - NetSuite - 将文件写入 Suitelet 响应以直接下载文件
- android - 由于 NestedScrollView,BottomAppBar 导航项不可点击
- tensorflow2.0 - GPflow 中的 Adam 优化器正在处理哪种类型的参数,受约束还是不受约束?