是否可以在JavaScript函数中定义全局变量?
我想在其他函数中使用trailimage变量(在makeObj函数中声明)。
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
<script type="text/javascript">
var offsetfrommouse = [10, -20];
var displayduration = 0;
var obj_selected = 0;
function makeObj(address) {
**var trailimage = [address, 50, 50];**
document.write('<img id="trailimageid" src="' + trailimage[0] + '" border="0" style=" position: absolute; visibility:visible; left: 0px; top: 0px; width: ' + trailimage[1] + 'px; height: ' + trailimage[2] + 'px">');
obj_selected = 1;
}
function truebody() {
return (!window.opera && document.compatMode && document.compatMode != "BackCompat") ? document.documentElement : document.body;
}
function hidetrail() {
var x = document.getElementById("trailimageid").style;
x.visibility = "hidden";
document.onmousemove = "";
}
function followmouse(e) {
var xcoord = offsetfrommouse[0];
var ycoord = offsetfrommouse[1];
var x = document.getElementById("trailimageid").style;
if (typeof e != "undefined") {
xcoord += e.pageX;
ycoord += e.pageY;
}
else if (typeof window.event != "undefined") {
xcoord += truebody().scrollLeft + event.clientX;
ycoord += truebody().scrollTop + event.clientY;
}
var docwidth = 1395;
var docheight = 676;
if (xcoord + trailimage[1] + 3 > docwidth || ycoord + trailimage[2] > docheight) {
x.display = "none";
alert("inja");
}
else
x.display = "";
x.left = xcoord + "px";
x.top = ycoord + "px";
}
if (obj_selected = 1) {
alert("obj_selected = true");
document.onmousemove = followmouse;
if (displayduration > 0)
setTimeout("hidetrail()", displayduration * 1000);
}
</script>
</head>
<body>
<form id="form1" runat="server">
<img alt="" id="house" src="Pictures/sides/right.gif" style="z-index: 1; left: 372px;
top: 219px; position: absolute; height: 138px; width: 120px" onclick="javascript:makeObj('Pictures/sides/sides-not-clicked.gif');" />
</form>
</body>
</html>
这取决于你对“全球”这个词的理解。如果你想要一个变量的全局作用域用于函数使用(这是OP想要的),那么你应该在编辑后阅读。如果希望在不使用服务器的情况下在页面之间重用数据,则应该使用sessionStorage。
会话存储
var Global = 'Global';
function LocalToGlobalVariable() {
// This creates a local variable.
var Local = '5';
// Doing this makes the variable available for one session
// (a page refresh - it's the session not local)
sessionStorage.LocalToGlobalVar = Local;
// It can be named anything as long as the sessionStorage
// references the local variable.
// Otherwise it won't work.
// This refreshes the page to make the variable take
// effect instead of the last variable set.
location.reload(false);
};
// This calls the variable outside of the function for whatever use you want.
sessionStorage.LocalToGlobalVar;
我知道这里面可能有很多语法错误,但这是大意……非常感谢LayZee指出这一点…您可以在http://www.w3schools.com/html/html5_webstorage.asp上找到什么是本地存储和会话存储。我需要同样的东西为我的代码,这是一个非常好的想法。
范围
在2015年,一个新的javascript“标准”(如果你愿意的话)被引入。这个标准为javascript引入了许多新思想,其中之一就是作用域的实现。
https://www.w3schools.com/js/js_scope.asp上有关于这一想法的所有细节,但cliff指出:
Const定义了一个常量。
Var具有“全局”作用域。
让有“函数”或“块”范围。
这取决于你对“全球”这个词的理解。如果你想要一个变量的全局作用域用于函数使用(这是OP想要的),那么你应该在编辑后阅读。如果希望在不使用服务器的情况下在页面之间重用数据,则应该使用sessionStorage。
会话存储
var Global = 'Global';
function LocalToGlobalVariable() {
// This creates a local variable.
var Local = '5';
// Doing this makes the variable available for one session
// (a page refresh - it's the session not local)
sessionStorage.LocalToGlobalVar = Local;
// It can be named anything as long as the sessionStorage
// references the local variable.
// Otherwise it won't work.
// This refreshes the page to make the variable take
// effect instead of the last variable set.
location.reload(false);
};
// This calls the variable outside of the function for whatever use you want.
sessionStorage.LocalToGlobalVar;
我知道这里面可能有很多语法错误,但这是大意……非常感谢LayZee指出这一点…您可以在http://www.w3schools.com/html/html5_webstorage.asp上找到什么是本地存储和会话存储。我需要同样的东西为我的代码,这是一个非常好的想法。
范围
在2015年,一个新的javascript“标准”(如果你愿意的话)被引入。这个标准为javascript引入了许多新思想,其中之一就是作用域的实现。
https://www.w3schools.com/js/js_scope.asp上有关于这一想法的所有细节,但cliff指出:
Const定义了一个常量。
Var具有“全局”作用域。
让有“函数”或“块”范围。
作为全局变量的替代方案,您可以使用浏览器的localstorage(如果不使用旧版本)。您最多可以使用5MB的存储空间。
代码是
var yourvariablevalue = 'this is a sample value';
localStorage.setItem("yourKeyvariablename", yourvariablevalue);
它可以在任何时候被取回,甚至在你离开并在其他时间打开它之后。
var yourvariablevalue = localStorage.getItem("yourKeyvariablename");
希望这对你有所帮助!
JavaScript中有三种作用域:
全局作用域:变量在代码中可用。
块作用域:变量在特定区域内可用,就像函数一样。
局部作用域:变量在更特定的区域可用,如if语句
如果在变量名之前添加Var,那么它的作用域将确定其位置
例子:
var num1 = 18; // Global scope
function fun() {
var num2 = 20; // Local (Function) Scope
if (true) {
var num3 = 22; // Block Scope (within an if-statement)
}
}
num1 = 18; // Global scope
function fun() {
num2 = 20; // Global Scope
if (true) {
num3 = 22; // Global Scope
}
}
只需在函数外部声明它,并在函数内部赋值即可。喜欢的东西:
<script type="text/javascript">
var offsetfrommouse = [10, -20];
var displayduration = 0;
var obj_selected = 0;
var trailimage = null ; // Global variable
function makeObj(address) {
trailimage = [address, 50, 50]; // Assign value
或者简单地从函数内部的变量名中删除“var”也会使它成为全局的,但为了代码更干净,最好在外部声明一次。这也可以:
var offsetfrommouse = [10, -20];
var displayduration = 0;
var obj_selected = 0;
function makeObj(address) {
trailimage = [address, 50, 50]; // Global variable, assign value
我希望这个例子能解释更多:http://jsfiddle.net/qCrGE/
var globalOne = 3;
testOne();
function testOne()
{
globalOne += 2;
alert("globalOne is :" + globalOne );
globalOne += 1;
}
alert("outside globalOne is: " + globalOne);
testTwo();
function testTwo()
{
globalTwo = 20;
alert("globalTwo is " + globalTwo);
globalTwo += 5;
}
alert("outside globalTwo is:" + globalTwo);