diff options
Diffstat (limited to 'files/he/web/javascript/reference/statements/var/index.html')
| -rw-r--r-- | files/he/web/javascript/reference/statements/var/index.html | 171 |
1 files changed, 0 insertions, 171 deletions
diff --git a/files/he/web/javascript/reference/statements/var/index.html b/files/he/web/javascript/reference/statements/var/index.html deleted file mode 100644 index 64020dee3e..0000000000 --- a/files/he/web/javascript/reference/statements/var/index.html +++ /dev/null @@ -1,171 +0,0 @@ ---- -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> |
