admin管理员组文章数量:1416315
I'm sure, this question has been answered somewhere before but I just couldn't find it.
If within a function a variable has been defined, what is the best practice to save it for later use? 1. Saving it "globally"?
foo = 'bar';
...
function bar(){
...
foo = 'bat';
return foo;
}
...
Here, the variable will be altered later on. 2. Or saving it within a hidden form field within the HTML-DOM?
`Thanxs!
I'm sure, this question has been answered somewhere before but I just couldn't find it.
If within a function a variable has been defined, what is the best practice to save it for later use? 1. Saving it "globally"?
foo = 'bar';
...
function bar(){
...
foo = 'bat';
return foo;
}
...
Here, the variable will be altered later on. 2. Or saving it within a hidden form field within the HTML-DOM?
`Thanxs!
Share Improve this question edited Oct 25, 2010 at 11:18 Mike asked Oct 25, 2010 at 10:59 MikeMike 231 silver badge4 bronze badges 6- Depends on how it is to be used and why. If its only to be used in the same function you could store it as a property on the function name bar["foo"] = "", but if it is to be used elsewhere it might be better to declare it there fro mthe beginning. – David Mårtensson Commented Oct 25, 2010 at 11:03
-
Note: I think you might want to edit it to say
foo = "bar"; function bar(){ ... foo = "bat"; return foo; }
. At least that is how I read the question. – BudgieInWA Commented Oct 25, 2010 at 11:07 - This is en exaple of how I thought: function c() { if(typeof c["1"] == "undefined") c["1"] = 1; document.write(c["1"]++); } c(); It works as a static variable. – David Mårtensson Commented Oct 25, 2010 at 11:10
- By the way, how do I highlight code in ments ;) – David Mårtensson Commented Oct 25, 2010 at 11:12
- @David Mårtensson, Backticks surrounding your code. (The key is probably just below your ESC key). Looks like this: ` (This can be used for inline code in Qs and As too) – BudgieInWA Commented Oct 25, 2010 at 11:20
3 Answers
Reset to default 2Saving it as a global JavaScript variable is by far the most efficient.
EDIT: If the data you want to save is associated with an element on the page (for example, each row in a table has a bit of data associated with it), and you are using jQuery, there is a data() method which is more efficient than setting an attribute on the element or something similar.
It depends on the context, but probably: In a variable defined at the top level of a closure that wraps the set of functions to which it applies.
i.e.
var exports = function () {
var stored_data;
function set_data(foo) {
stored_data = foo;
}
function get_data() {
return stored_data;
}
return { get: get_data, set: set_data };
}();
This avoids the risk of other scripts (or other parts of your own, potentially very large, script) overwriting it by accident.
The HTML5 spec has defined a solution to this question: If you are using HTML5, you can specify data attributes in your DOM.
See this page for more info: http://ejohn/blog/html-5-data-attributes/
This is now the standardised way of doing it, so I guess that it's considered best practice. Also John Resig, who wrote the blog I linked to above, is the author of JQuery, so if it's good enough for him, who am I to argue.
The really good news is that you don't even have to be using an HTML5-patible browser for this technique to work - it already works in older browsers; it's just that now it's been encoded into the standard, and there's a defined way to do it.
That said, there's nothing wrong with a global variable in your Javascript as long as you avoid polluting the namespace too much, and it would be more efficient from a performance perspective, so there's plenty of merit in that approach as well.
本文标签: Best practice JavascriptJquery saving variable for later useStack Overflow
版权声明:本文标题:Best practice: JavascriptJquery saving variable for later use - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745243663a2649456.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论