diff options
| author | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:41:45 -0500 |
|---|---|---|
| committer | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:41:45 -0500 |
| commit | 1109132f09d75da9a28b649c7677bb6ce07c40c0 (patch) | |
| tree | 0dd8b084480983cf9f9680e8aedb92782a921b13 /files/he/web/javascript/reference/statements/const/index.html | |
| parent | 4b1a9203c547c019fc5398082ae19a3f3d4c3efe (diff) | |
| download | translated-content-1109132f09d75da9a28b649c7677bb6ce07c40c0.tar.gz translated-content-1109132f09d75da9a28b649c7677bb6ce07c40c0.tar.bz2 translated-content-1109132f09d75da9a28b649c7677bb6ce07c40c0.zip | |
initial commit
Diffstat (limited to 'files/he/web/javascript/reference/statements/const/index.html')
| -rw-r--r-- | files/he/web/javascript/reference/statements/const/index.html | 133 |
1 files changed, 133 insertions, 0 deletions
diff --git a/files/he/web/javascript/reference/statements/const/index.html b/files/he/web/javascript/reference/statements/const/index.html new file mode 100644 index 0000000000..98de11967b --- /dev/null +++ b/files/he/web/javascript/reference/statements/const/index.html @@ -0,0 +1,133 @@ +--- +title: const +slug: Web/JavaScript/Reference/Statements/const +tags: + - const + - מה זה const + - משתנים +translation_of: Web/JavaScript/Reference/Statements/const +--- +<div>{{jsSidebar("Statements")}}</div> + +<div>מילת ההצהרה <strong><code>const</code></strong> משמשת להכזרה על משתנה קבוע שאין אפשרות לשנות את הערך שלו.<br> + </div> + +<div>{{EmbedInteractiveExample("pages/js/statement-const.html")}}</div> + + + +<h2 id="תחביר">תחביר</h2> + +<pre class="syntaxbox">const <em>name1 = <em>value1 [</em>, <em>name2</em> = <em>value2</em><em> [</em>, ... [</em>, <em>nameN</em> = <em>valueN]]]</em>;</pre> + +<dl> + <dt><code>nameN</code></dt> + <dd>שם המשתנה.</dd> + <dt><code>valueN</code></dt> + <dd>הערך של המשתנה.</dd> +</dl> + +<h2 id="תיאור">תיאור</h2> + +<p>בשפת ג'אווה סקריפט אנו משתמשים במשתנים על מנת להחזיק ערכים שונים.<br> + הצהרה על משתנה באמצעות <strong><code>const</code></strong> הופכת אותו לקבוע ולא ניתן לשנות את הערך שלו.<br> + הקצאת ערך למשתנה ללא הצהרה מראש הופכת אותו למשתנה גלובלי, אך בשונה מהצהרה באמצעות <strong><code>var</code></strong> הוא אינו כפוף לאובייקט האב <strong><code>window</code></strong>.<br> + חשוב לציין שהצהרה באמצעות const לא מבצעת <a href="/he/docs/">Hoisting</a>.</p> + +<h2 id="דוגמאות">דוגמאות</h2> + +<p>הדוגמה הבאה ממחישה כיצד מתנהגים משתנים קבועים.</p> + +<pre class="brush:js">// NOTE: Constants can be declared with uppercase or lowercase, but a common +// convention is to use all-uppercase letters. + +// define MY_FAV as a constant and give it the value 7 +const MY_FAV = 7; + +// this will throw an error - Uncaught TypeError: Assignment to constant variable. +MY_FAV = 20; + +// MY_FAV is 7 +console.log('my favorite number is: ' + MY_FAV); + +// trying to redeclare a constant throws an error - Uncaught SyntaxError: Identifier 'MY_FAV' has already been declared +const MY_FAV = 20; + +// the name MY_FAV is reserved for constant above, so this will fail too +var MY_FAV = 20; + +// this throws an error too +let MY_FAV = 20; + +// it's important to note the nature of block scoping +if (MY_FAV === 7) { + // this is fine and creates a block scoped MY_FAV variable + // (works equally well with let to declare a block scoped non const variable) + let MY_FAV = 20; + + // MY_FAV is now 20 + console.log('my favorite number is ' + MY_FAV); + + // this gets hoisted into the global context and throws an error + var MY_FAV = 20; +} + +// MY_FAV is still 7 +console.log('my favorite number is ' + MY_FAV); + +// throws an error - Uncaught SyntaxError: Missing initializer in const declaration +const FOO; + +// const also works on objects +const MY_OBJECT = {'key': 'value'}; + +// Attempting to overwrite the object throws an error - Uncaught TypeError: Assignment to constant variable. +MY_OBJECT = {'OTHER_KEY': 'value'}; + +// However, object keys are not protected, +// so the following statement is executed without problem +MY_OBJECT.key = 'otherValue'; // Use Object.freeze() to make object immutable + +// The same applies to arrays +const MY_ARRAY = []; +// It's possible to push items into the array +MY_ARRAY.push('A'); // ["A"] +// However, assigning a new array to the variable throws an error - Uncaught TypeError: Assignment to constant variable. +MY_ARRAY = ['B'];</pre> + +<h2 id="מפרט">מפרט</h2> + +<table class="standard-table"> + <thead> + <tr> + <th scope="col">Specification</th> + <th scope="col">Status</th> + <th scope="col">Comment</th> + </tr> + </thead> + <tbody> + <tr> + <td>{{SpecName('ES2015', '#sec-let-and-const-declarations', 'Let and Const Declarations')}}</td> + <td>{{Spec2('ES2015')}}</td> + <td>Initial definition.</td> + </tr> + <tr> + <td>{{SpecName('ESDraft', '#sec-let-and-const-declarations', 'Let and Const Declarations')}}</td> + <td>{{Spec2('ESDraft')}}</td> + <td>No changes.</td> + </tr> + </tbody> +</table> + +<h2 id="תאימות_דפדפן">תאימות דפדפן</h2> + + + +<p>{{Compat("javascript.statements.const")}}</p> + +<h2 id="ראה_גם">ראה גם</h2> + +<ul> + <li><a href="https://developer.mozilla.org/he/docs/Web/JavaScript/Reference/Statements/var"><code>var</code></a></li> + <li><a href="https://developer.mozilla.org/he/docs/Web/JavaScript/Reference/Statements/let"><code>let</code></a></li> +</ul> |
