简体   繁体   English

结合两个javascript函数

[英]combining two javascript functions

Are there any benefits of combining the below functions? 组合以下功能有什么好处?

window.onload = function() {
  if (localStorage.getItem("txt")) {
    textarea.value = localStorage.getItem("txt");
    changeDocTitle(localStorage.getItem("filename"));
    isModified = true;
  } else {
    changeDocTitle(untitled);
  }
};

function newNote() {
  if (!isModified || dontSave()) {
    textarea.value = "";
    changeDocTitle(untitled);
  }
  textarea.focus();
}

After combining it will look like this: 合并后将如下所示:

window.onload = function() {
  if (localStorage.getItem("txt")) {
    newNote(localStorage.getItem("txt"), localStorage.getItem("filename"));
  } else {
    newNote();
  }
};

function newNote(txt, filename) {
  if (!isModified || dontSave()) {
    textarea.value = txt || "";
    changeDocTitle(filename || untitled);
    if (textarea.value) {
      isModified = true;
    }
  }
  textarea.focus();
}
  • I'll call the newNote() function with keyboard shortcuts too.. 我也将使用键盘快捷键来调用newNote()函数。

What is the difference between the two, and is there any reason to prefer one over the other? 两者之间有什么区别,有什么理由优先选择另一个?

The second one. 第二个。

If the scope of newNote is limited to the onload function, then there is no reason to dirty the global scope. 如果newNote的范围仅限于onload函数,则没有理由弄脏全局范围。


EDIT 编辑

Frankly, it doesn't matter much. 坦白说,没关系。 If you binding the keyboard event, then the function will remain in scope throughout the document scope. 如果绑定键盘事件,则该功能将保留在整个文档范围内。 So either would be good. 因此,两者都将是一件好事。

Besides, questions in SO, are more problem oriented. 此外,SO中的问题更倾向于问题。 Try https://codereview.stackexchange.com/ , instead. 请尝试https://codereview.stackexchange.com/

This may be primarily opinion-based, but I would go with the second one. 这可能主要基于观点,但我会讲第二点。
Reasons: 原因:

Increased Readability and Easier Maintainance 可读性更高,维护更轻松

It's obviously easier to read and maintain one function instead of two, and you keep your code in one place altogether. 显然,读取和维护一个功能而不是两个功能更容易,而且您将代码完全放在一个位置。

No waste of resources 不浪费资源

You create the variables you need in your function and they go away when you are done with it. 您可以在函数中创建所需的变量,完成后它们就会消失。

Warning, though. 但是警告。

You should know that there are certain things to avoid. 您应该知道应该避免某些事情。 For example, if your newNote function will require more and more arguments over time, you may consider changing the scope, and possibly maintain it out of the function to avoid having a function with (let's say) a dozen arguments, because you just lose the benefits listed above. 例如,如果您的newNote函数随着时间的推移将需要越来越多的参数,则可以考虑更改范围,并可能将其保留在该函数之外,以避免具有(例如)十几个参数的函数,因为您只是失去了上面列出的好处。

PS: I think the second code you described has some errors. PS:我认为您描述的第二个代码有一些错误。 Unless you just wrote it like that to explain what you intend to do. 除非您只是这样写,否则说明您打算做什么。 (I'm talking about textarea.value = txt || ""; ) (我说的是textarea.value = txt || "";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM