Var&;JavaScript变量范围
JS代码Var&;JavaScript变量范围,javascript,html,variables,global-variables,scope,Javascript,Html,Variables,Global Variables,Scope,JS代码 var foo = "Hello World!"; // <------- global scope document.write("<p>Before our anonymous function foo means '" + foo + '".</p>'); (function() { // The following code will be enclosed within an anonymous func
var foo = "Hello World!"; // <------- global scope
document.write("<p>Before our anonymous function foo means '" + foo + '".</p>');
(function() {
// The following code will be enclosed within an anonymous function
var foo = "Goodbye World!"; // <------- local scope
document.write("<p>Inside our anonymous function foo means '" + foo + '".</p>');
})(); // We call our anonymous function immediately
document.write("<p>After our anonymous function foo means '" + foo + '".</p>');
我的问题是
- 当我们在函数中替换
变量的值时,为什么 不换吗?它如何仍然包含foo
“Hello World!”
- 如果我要访问函数中的全局变量,我该怎么做
var
语句,您将更改全局变量:
var foo = "Hello World!"; // <------- global scope
document.write("<p>Before our anonymous function foo means '" + foo + '".</p>');
(function() {
// The following code will be enclosed within an anonymous function
foo = "Goodbye World!"; // <------- local scope
document.write("<p>Inside our anonymous function foo means '" + foo + '".</p>');
})(); // We call our anonymous function immediately
document.write("<p>After our anonymous function foo means '" + foo + '".</p>');
var foo=“你好,世界!”;// 从匿名函数中删除var
语句,您将更改全局变量:
var foo = "Hello World!"; // <------- global scope
document.write("<p>Before our anonymous function foo means '" + foo + '".</p>');
(function() {
// The following code will be enclosed within an anonymous function
foo = "Goodbye World!"; // <------- local scope
document.write("<p>Inside our anonymous function foo means '" + foo + '".</p>');
})(); // We call our anonymous function immediately
document.write("<p>After our anonymous function foo means '" + foo + '".</p>');
var foo=“你好,世界!”;// 通过在函数中使用var foo
,可以明确地告诉变量仅在该函数中局部更改。如果您想全局更改它,只需使用foo=…
如果您想了解它,我建议您在函数中使用var foo
,明确告诉变量仅在该函数中局部更改。如果您想全局更改它,只需使用foo=…
如果您想了解它,我建议您使用函数范围的变量来隐藏全局变量。要访问全局变量,可以使用window.foo显式地这样说
var foo = "Hello World!"; // <------- global scope
document.write("<p>Before our anonymous function foo means '" + foo + '".</p>');
(function() {
// The following code will be enclosed within an anonymous function
var foo = "Goodbye World!"; // <------- local scope
document.write("<p>Inside our anonymous function foo means '" + foo + '".</p>');
document.write("<p>Inside our anonymous function window.foo means '" + window.foo + '".</p>');
window.foo = window.foo + foo;
})(); // We call our anonymous function immediately
document.write("<p>After our anonymous function foo means '" + foo + '".</p>');
var foo=“你好,世界!”;// 您正在使用函数范围的变量来隐藏全局变量。要访问全局变量,可以使用window.foo显式地这样说
var foo = "Hello World!"; // <------- global scope
document.write("<p>Before our anonymous function foo means '" + foo + '".</p>');
(function() {
// The following code will be enclosed within an anonymous function
var foo = "Goodbye World!"; // <------- local scope
document.write("<p>Inside our anonymous function foo means '" + foo + '".</p>');
document.write("<p>Inside our anonymous function window.foo means '" + window.foo + '".</p>');
window.foo = window.foo + foo;
})(); // We call our anonymous function immediately
document.write("<p>After our anonymous function foo means '" + foo + '".</p>');
var foo=“你好,世界!”;//在所有的编码语言中不是都一样吗?作用域和内部作用域并不是js独有的行为,尽管它具有“唯一性”。在所有编码语言中不是都一样吗?作用域和内部作用域不是很独特的行为,尽管它具有“唯一性”。O/P在我们的匿名函数窗口中。foo表示“未定义”。var=“Hello World!“将其添加到窗口对象,使其可以从匿名函数中访问。下面是一篇关于窗口范围的帖子,O/P位于我们的匿名函数窗口中。foo表示“未定义”。var=“Hello World!”的全局删除将其添加到窗口对象,使其可以从匿名函数中访问。这里有一篇关于窗口范围的帖子