aboutsummaryrefslogtreecommitdiff
path: root/files/he/web/javascript/reference/statements/var/index.html
blob: 64020dee3ed58672d419c4331cd6da9ee5e5191c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
---
title: var
slug: Web/JavaScript/Reference/Statements/var
tags:
  - hoisted
  - הכרזה
  - משתנה
translation_of: Web/JavaScript/Reference/Statements/var
---
<div>{{jsSidebar("Statements")}}</div>

<p>מילת ההצהרה <strong><code>var</code></strong> משמשת להכזרה על משתנה.</p>

<div>{{EmbedInteractiveExample("pages/js/statement-var.html")}}</div>



<h2 id="תחביר">תחביר</h2>

<pre class="syntaxbox">var <em>varname1 [</em>= <em>value1] [</em>, <em>varname2 [</em>= <em>value2] </em><em>... [</em>, <em>varnameN [</em>= <em>valueN]]]</em>;</pre>

<dl>
 <dt><code>varnameN</code></dt>
 <dd>שם המשתנה.</dd>
</dl>

<dl>
 <dt><code>valueN</code></dt>
 <dd> הערך של המשתנה.</dd>
</dl>

<h2 id="תיאור">תיאור</h2>

<p>בשפת ג'אווה סקריפט אנו משתמשים במשתנים על מנת להחזיק ערכים שונים.<br>
 הקצאת ערך למשתנה ללא הצהרה מראש הופכת אותו למשתנה כגלובלי.<br>
 ניתן להשתמש במשתנה לפני שהוכרז, השימוש יעשה באמצעות Hoisting.</p>

<p><strong>מהם ההבדלים בין משתנים מוצהרים לאלה שאינם?</strong><br>
 1. ראשית, משתנים מוצהרים יעשו רק את הפעולה שלשמה נוצרו, לעומת משתנים לא מוצהרים הנחשבים גלובלים.</p>

<pre class="brush: js">function x() {
  y = 1;   // Throws a ReferenceError in strict mode
  var z = 2;
}

x();

console.log(y); // logs "1"
console.log(z); // Throws a ReferenceError: z is not defined outside x
</pre>

<p>2. משתנים מוצהרים מוכרזים לפני ביצוע קוד כלשהו לעומת זאת משתנים לא מוצהרים אינם קיימים עד שהקוד שמכריז עליהם מתבצע.</p>

<pre class="brush: js">console.log(a);                // Throws a ReferenceError.
console.log('still going...'); // Never executes.</pre>

<pre class="brush: js">var a;
console.log(a);                // logs "undefined" or "" depending on browser.
console.log('still going...'); // logs "still going...".</pre>

<p>בשל שני ההבדלים הללו, אי הכרזה על משתנים עשויה להוביל לשגיאות בלתי צפויות.<br>
 לכן, מומלץ תמיד להכריז על משתנים, גם אם הם נמצאים בפונקציה.</p>

<h3 id="var_hoisting">var hoisting</h3>

<p>זוהי התנהגות ברירת המחדל של השפה, שתפקידה להעביר את כל ההצהרות לחלק העליון של הסקריפט או הפונקציה ולכן משמעות הדבר היא שניתן להשתמש במשתנה לפני שהוכרז.</p>

<pre class="brush: js">bla = 2;
var bla;
// ...

// is implicitly understood as:

var bla;
bla = 2;
</pre>

<p>מומלץ להצהיר על משתנים בחלקו העליון של הסקריפט או הפונקציה וכך יהיה ברור אילו משתנים שייכים לפונקציה באופן מקומי ואילו גלובלים.</p>

<p>חשוב לזכור ששימוש ב-Hoisting ישפיע על הצהרת המשתנה אך לא על אתחול הערך:</p>

<pre class="brush: js">function do_something() {
  console.log(bar); // undefined
  var bar = 111;
  console.log(bar); // 111
}

// is implicitly understood as:
function do_something() {
  var bar;
  console.log(bar); // undefined
  bar = 111;
  console.log(bar); // 111
}
</pre>

<h2 id="דוגמאות">דוגמאות</h2>

<h3 id="הכרזה_אחת_של_שני_משתנים">הכרזה אחת של שני משתנים</h3>

<pre class="brush: js" dir="rtl">var a = 0, b = 0;
</pre>

<h3 id="הקצאת_שני_משתנים_עם_ערך_מחרוזת_יחיד">הקצאת שני משתנים עם ערך מחרוזת יחיד</h3>

<pre class="brush: js">var a = 'A';
var b = a;

// Equivalent to:

var a, b = a = 'A';
</pre>

<h3 id="משתנה_מקומי_וגלובלי">משתנה מקומי וגלובלי</h3>

<pre class="brush: js">var x = 0;

function f() {
  var x = y = 1; // x is declared locally. y is not!
}
f();

console.log(x, y); // Throws a ReferenceError in strict mode (y is not defined). 0, 1 otherwise.
// In non-strict mode:
// x is the global one as expected
// y leaked outside of the function, though!</pre>

<h2 id="מפרט">מפרט</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
  <tr>
   <td>{{SpecName('ES1')}}</td>
   <td>{{Spec2('ES1')}}</td>
   <td>Initial definition. Implemented in JavaScript 1.0</td>
  </tr>
  <tr>
   <td>{{SpecName('ES5.1', '#sec-12.2', 'var statement')}}</td>
   <td>{{Spec2('ES5.1')}}</td>
   <td> </td>
  </tr>
  <tr>
   <td>{{SpecName('ES6', '#sec-variable-statement', 'variable statement')}}</td>
   <td>{{Spec2('ES6')}}</td>
   <td> </td>
  </tr>
  <tr>
   <td>{{SpecName('ESDraft', '#sec-variable-statement', 'variable statement')}}</td>
   <td>{{Spec2('ESDraft')}}</td>
   <td> </td>
  </tr>
 </tbody>
</table>

<h2 id="תאימות_דפדפן">תאימות דפדפן</h2>



<p>{{Compat("javascript.statements.var")}}</p>

<h2 id="ראה_גם">ראה גם</h2>

<ul>
 <li><a href="https://developer.mozilla.org/he/docs/Web/JavaScript/Reference/Statements/let"><code>let</code></a></li>
 <li><a href="https://developer.mozilla.org/he/docs/Web/JavaScript/Reference/Statements/const"><code>const</code></a></li>
</ul>