function keyword in - قم بتعريف المتغير الشامل في وظيفة JavaScript
تحديث 1: إذا كنت تقرأ التعليقات هناك مناقشة لطيفة حول هذا العرف تسمية الاسم.
تحديث 2: يبدو أنه منذ أن تم نشر إجابتي أصبحت اتفاقية التسمية أكثر رسمية. الناس الذين يعلمون ، وكتابة الكتب وما إلى ذلك يتكلم عن var
، وإعلان function
.
UPDATE3: هنا هو وظيفة ويكيبيديا الإضافية التي تدعم وجهة نظري: http://en.wikipedia.org/wiki/Declaration_(computer_programming)#Declarations_and_Definitions
... والإجابة على السؤال الرئيسي. DECLARE متغير قبل وظيفتك. سيعمل هذا وسيتوافق مع الممارسة الجيدة لإعلان المتغيرات الخاصة بك في الجزء العلوي من النطاق :)
هل من الممكن تحديد متغير عام في وظيفة 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>
لا ، لا يمكنك ذلك. فقط أعلن المتغير خارج الوظيفة. لا يتعين عليك التصريح بها في نفس الوقت الذي تحدد فيه القيمة:
var trailimage;
function makeObj(address) {
trailimage = [address, 50, 50];
وهو بسيط للغاية تعريف متغير trailimage خارج الدالة وتعيين قيمتها في وظيفة makeObj. الآن يمكنك الوصول إلى قيمته من أي مكان.
var offsetfrommouse = [10, -20];
var displayduration = 0;
var obj_selected = 0;
var trailimage;
function makeObj(address) {
trailimage = [address, 50, 50];
....
}
في ما يلي نموذج لشفرة يمكن أن تكون مفيدة.
var Human = function(){
name = "Shohanur Rahaman"; // global variable
this.name = "Tuly"; // constructor variable
var age = 21;
};
var shohan = new Human();
document.write(shohan.name+"<br>");
document.write(name);
document.write(age); // undefined cause its local variable
هنا وجدت إجابة لطيفة How-can-one-declare-a-global-variable-in-JavaScript
إذا كنت تقوم بعمل وظيفة بدء التشغيل ، يمكنك تحديد الوظائف والمتغيرات العالمية بهذه الطريقة:
function(globalScope)
{
//define something
globalScope.something() {
alert("It works");
};
}(window)
نظرًا لأن الدالة يتم استدعاؤها عالميًا باستخدام هذه الوسيطة ، فهذا هو النطاق العالمي هنا. لذا ، يجب أن يكون الشيء شيء عالمي.