From 1109132f09d75da9a28b649c7677bb6ce07c40c0 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:41:45 -0500 Subject: initial commit --- .../reference/statements/const/index.html | 133 ++++++++++ .../reference/statements/for...of/index.html | 269 +++++++++++++++++++++ .../reference/statements/for_each...in/index.html | 128 ++++++++++ .../reference/statements/function_star_/index.html | 212 ++++++++++++++++ .../web/javascript/reference/statements/index.html | 148 ++++++++++++ .../reference/statements/return/index.html | 151 ++++++++++++ .../javascript/reference/statements/var/index.html | 171 +++++++++++++ 7 files changed, 1212 insertions(+) create mode 100644 files/he/web/javascript/reference/statements/const/index.html create mode 100644 files/he/web/javascript/reference/statements/for...of/index.html create mode 100644 files/he/web/javascript/reference/statements/for_each...in/index.html create mode 100644 files/he/web/javascript/reference/statements/function_star_/index.html create mode 100644 files/he/web/javascript/reference/statements/index.html create mode 100644 files/he/web/javascript/reference/statements/return/index.html create mode 100644 files/he/web/javascript/reference/statements/var/index.html (limited to 'files/he/web/javascript/reference/statements') 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 +--- +
{{jsSidebar("Statements")}}
+ +
מילת ההצהרה const משמשת להכזרה על משתנה קבוע שאין אפשרות לשנות את הערך שלו.
+ +
{{EmbedInteractiveExample("pages/js/statement-const.html")}}
+ + + +

תחביר

+ +
const name1 = value1 [, name2 = value2 [, ... [, nameN = valueN]]];
+ +
+
nameN
+
שם המשתנה.
+
valueN
+
הערך של המשתנה.
+
+ +

תיאור

+ +

בשפת ג'אווה סקריפט אנו משתמשים במשתנים על מנת להחזיק ערכים שונים.
+ הצהרה על משתנה באמצעות const הופכת אותו לקבוע ולא ניתן לשנות את הערך שלו.
+ הקצאת ערך למשתנה ללא הצהרה מראש הופכת אותו למשתנה גלובלי, אך בשונה מהצהרה באמצעות var הוא אינו כפוף לאובייקט האב window.
+ חשוב לציין שהצהרה באמצעות const לא מבצעת Hoisting.

+ +

דוגמאות

+ +

הדוגמה הבאה ממחישה כיצד מתנהגים משתנים קבועים.

+ +
// 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'];
+ +

מפרט

+ + + + + + + + + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('ES2015', '#sec-let-and-const-declarations', 'Let and Const Declarations')}}{{Spec2('ES2015')}}Initial definition.
{{SpecName('ESDraft', '#sec-let-and-const-declarations', 'Let and Const Declarations')}}{{Spec2('ESDraft')}}No changes.
+ +

תאימות דפדפן

+ + + +

{{Compat("javascript.statements.const")}}

+ +

ראה גם

+ + diff --git a/files/he/web/javascript/reference/statements/for...of/index.html b/files/he/web/javascript/reference/statements/for...of/index.html new file mode 100644 index 0000000000..86edb2f69e --- /dev/null +++ b/files/he/web/javascript/reference/statements/for...of/index.html @@ -0,0 +1,269 @@ +--- +title: for...of +slug: Web/JavaScript/Reference/Statements/for...of +translation_of: Web/JavaScript/Reference/Statements/for...of +--- +
{{jsSidebar("Statements")}}
+ +
 
+ +
הצהרת for...of יוצרת לולאה ע"ג iterable objects (שכולל {{jsxref("Array")}}, {{jsxref("Map")}}, {{jsxref("Set")}}, {{jsxref("String")}}, {{jsxref("TypedArray")}}, arguments object וכן הלאה..),
+ +
invoking a custom iteration hook with statements to be executed for the value of each distinct property.
+ +

Syntax

+ +
for (variable of iterable) {
+  statement
+}
+
+ +
+
variable
+
בכל איטרציה, ערך ה property הנוכחי של iterable מוקצה ל variable
+
iterable
+
אובייקט עם properties ברי איטרציה
+
+ +

לדוגמא:

+ +

איטרציה ע"ג {{jsxref("Array")}}

+ +
let iterable = [10, 20, 30];
+
+for (let value of iterable) {
+  console.log(value);
+}
+// 10
+// 20
+// 30
+
+ +

אפשר להשתמש ג"כ ב const במקום let באם אתה לא משנה את ערך המשתנה בתוך הבלוק.

+ +

 

+ +

איטרציה ע"ג {{jsxref("String")}}

+ +
let iterable = "boo";
+
+for (let value of iterable) {
+  console.log(value);
+}
+// "b"
+// "o"
+// "o"
+
+ +

Iterating over a {{jsxref("TypedArray")}}

+ +
let iterable = new Uint8Array([0x00, 0xff]);
+
+for (let value of iterable) {
+  console.log(value);
+}
+// 0
+// 255
+
+ +

Iterating over a {{jsxref("Map")}}

+ +
let iterable = new Map([["a", 1], ["b", 2], ["c", 3]]);
+
+for (let entry of iterable) {
+  console.log(entry);
+}
+// [a, 1]
+// [b, 2]
+// [c, 3]
+
+for (let [key, value] of iterable) {
+  console.log(value);
+}
+// 1
+// 2
+// 3
+
+ +

Iterating over a {{jsxref("Set")}}

+ +
let iterable = new Set([1, 1, 2, 2, 3, 3]);
+
+for (let value of iterable) {
+  console.log(value);
+}
+// 1
+// 2
+// 3
+
+ +

Iterating over a DOM collection

+ +

Iterating over DOM collections like {{domxref("NodeList")}}: the following example adds a read class to paragraphs that are direct descendants of an article:

+ +
// Note: This will only work in platforms that have
+// implemented NodeList.prototype[Symbol.iterator]
+let articleParagraphs = document.querySelectorAll("article > p");
+
+for (let paragraph of articleParagraphs) {
+  paragraph.classList.add("read");
+}
+
+ +

Iterating over generators

+ +

You can also iterate over generators:

+ +
function* fibonacci() { // a generator function
+  let [prev, curr] = [1, 1];
+  while (true) {
+    [prev, curr] = [curr, prev + curr];
+    yield curr;
+  }
+}
+
+for (let n of fibonacci()) {
+  console.log(n);
+  // truncate the sequence at 1000
+  if (n >= 1000) {
+    break;
+  }
+}
+
+ +

Iterating over other iterable objects

+ +

You can also iterate over an object that explicitly implements iterable protocol:

+ +
var iterable = {
+  [Symbol.iterator]() {
+    return {
+      i: 0,
+      next() {
+        if (this.i < 3) {
+          return { value: this.i++, done: false };
+        }
+        return { value: undefined, done: true };
+      }
+    };
+  }
+};
+
+for (var value of iterable) {
+  console.log(value);
+}
+// 0
+// 1
+// 2
+
+ +

Difference between for...of and for...in

+ +

The for...in loop will iterate over all enumerable properties of an object.

+ +

The for...of syntax is specific to collections, rather than all objects. It will iterate in this manner over the elements of any collection that has a [Symbol.iterator] property.

+ +

The following example shows the difference between a for...of loop and a for...in loop.

+ +
Object.prototype.objCustom = function () {};
+Array.prototype.arrCustom = function () {};
+
+let iterable = [3, 5, 7];
+iterable.foo = "hello";
+
+for (let i in iterable) {
+  console.log(i); // logs 0, 1, 2, "foo", "arrCustom", "objCustom"
+}
+
+for (let i of iterable) {
+  console.log(i); // logs 3, 5, 7
+}
+
+ +

Specifications

+ + + + + + + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('ES6', '#sec-for-in-and-for-of-statements', 'for...of statement')}}{{Spec2('ES6')}}Initial definition.
{{SpecName('ESDraft', '#sec-for-in-and-for-of-statements', 'for...of statement')}}{{Spec2('ESDraft')}} 
+ +

Browser compatibility

+ +

{{CompatibilityTable}}

+ +
+ + + + + + + + + + + + + + + + + + + +
FeatureChromeFirefox (Gecko)EdgeOperaSafari
Basic support{{CompatChrome(38)}} [1]
+ {{CompatChrome(51)}} [3]
{{CompatGeckoDesktop("13")}} [2]12257.1
+
+ +
+ + + + + + + + + + + + + + + + + + + + + +
FeatureAndroidChrome for AndroidFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
Basic support5.1{{CompatChrome(38)}} [1]{{CompatGeckoMobile("13")}} [2]{{CompatNo}}{{CompatUnknown}}8
+
+ +

[1] From Chrome 29 to Chrome 37 this feature was available behind a preference. In chrome://flags/#enable-javascript-harmony, activate the entry “Enable Experimental JavaScript”.

+ +

[2] Prior Firefox 51, using the for...of loop construct with the const keyword threw a {{jsxref("SyntaxError")}} ("missing = in const declaration"). This has been fixed ({{bug(1101653)}}).

+ +

[3] Support for iteration of objects was added in Chrome 51.

+ +

See also

+ + diff --git a/files/he/web/javascript/reference/statements/for_each...in/index.html b/files/he/web/javascript/reference/statements/for_each...in/index.html new file mode 100644 index 0000000000..b45c6f5ab7 --- /dev/null +++ b/files/he/web/javascript/reference/statements/for_each...in/index.html @@ -0,0 +1,128 @@ +--- +title: for each...in +slug: Web/JavaScript/Reference/Statements/for_each...in +translation_of: Archive/Web/JavaScript/for_each...in +--- +
{{jsSidebar("Statements")}}
+ +
+

טענת "for each...in" הוצאה משימוש בסטנדרט (ECMA-357 (E4X. התמיכה ב-E4X אמנם הוסרה, אך טענת ה"for each...in" לא תבוטל ולא תוסר מטעמי תאימות לגרסאות קודמות. עם זאת, מומלץ להשתמש בטענת "for...of" במקום. (ע"ע {{ bug("791343")}} ). 

+
+ +

טענה מסוג:

+ +

for each...in

+ +

("עבור כל... ב...")

+ +

חגה סביב  כל הערכים של כל "פריטי הרכוש" (properties) של משתנה נתון. עבור כל אחד מפריטי הרכוש תבוצע טענה מוגדרת.

+ +

תחביר

+ +
for each (variable in object) {
+  statement
+}
+ +
+
variable
+
המשתנה שיחוג סביב הערכים של פרטי הרכוש. ניתן (אך לא חובה)  להצהיר על משתנה זה עם מילת המפתח var. המשתנה הזה הוא פנימי לפונקציה, ולא ללולאה עצמה.
+
+ +
+
object
+
האובייקט שסביב פריטי הרכוש שלו יש לחוג. 
+
+ +
+
statement
+
טענה שיש לבצע עבור כל אחד מפריטי הרכוש. על מנת לבצע יותר מטענה אחת בתוך הלולאה, יש להשתמש בטענת בלוק ({ ... }) כדי לקבץ את הטענות הללו יחדיו.
+
+ +

תיאור

+ +

 

+ +

Some built-in properties are not iterated over. These include all built-in methods of objects, e.g. String's indexf

+ +

method. However, all user-defined properties are iterated over.

+ +

Examples

+ +

Using for each...in

+ +

Warning: Never use a loop like this on arrays. Only use it on objects. See for...in for more details.

+ +

The following snippet iterates over an object's properties, calculating their sum:

+ +
var sum = 0;
+var obj = {prop1: 5, prop2: 13, prop3: 8};
+
+for each (var item in obj) {
+  sum += item;
+}
+
+console.log(sum); // logs "26", which is 5+13+8
+ +

Specifications

+ +

Not part of a current ECMA-262 specification. Implemented in JavaScript 1.6 and deprecated.

+ +

Browser compatibility

+ +

{{CompatibilityTable}}

+ +
+ + + + + + + + + + + + + + + + + + + +
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari
Basic support{{CompatNo}}{{CompatGeckoDesktop("1.8")}}{{CompatNo}}{{CompatNo}}{{CompatNo}}
+
+ +
+ + + + + + + + + + + + + + + + + + + + + +
FeatureAndroidChrome for AndroidFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
Basic support{{CompatNo}}{{CompatNo}}{{CompatGeckoMobile("1.0")}}{{CompatNo}}{{CompatNo}}{{CompatNo}}
+
+ +

See also

+ + diff --git a/files/he/web/javascript/reference/statements/function_star_/index.html b/files/he/web/javascript/reference/statements/function_star_/index.html new file mode 100644 index 0000000000..a72a927859 --- /dev/null +++ b/files/he/web/javascript/reference/statements/function_star_/index.html @@ -0,0 +1,212 @@ +--- +title: function* +slug: Web/JavaScript/Reference/Statements/function* +translation_of: Web/JavaScript/Reference/Statements/function* +--- +
{{jsSidebar("Statements")}}
+ +

 

+ +

הצהרת function* (מילת function ולאחריה כוכבית) מגדירה פונקצית גנרטור, אשר מחזירה אובייקט {{jsxref("Global_Objects/Generator","Generator")}}.

+ +
{{EmbedInteractiveExample("pages/js/statement-functionasterisk.html")}}
+ + + +
+

ניתן גם להגדיר פונקציות גנרטור על-ידי שימוש בבנאי {{jsxref("GeneratorFunction")}}, או בתחביר של ביטוי פונקציה.

+
+ +

תחביר

+ +
function* name([param[, param[, ... param]]]) {
+   statements
+}
+
+ +
+
name
+
שם הפונקציה.
+
+ +
+
param
+
השם של פרמטר רשמי של הפונקציה.
+
+ +
+
statements
+
הפקודות המרכיבות את גוף הפונקציה.
+
+ +

תיאור

+ +

גנרטורים הינם פונקציות שניתן לצאת מהן ולאחר מכן להיכנס אליהן מחדש. ההקשר שלהם (קשירת המשתנים) יישמר לאורך הכניסות מחדש.
+
+ גנרטורים ב- JavaScript -- במיוחד בשילוב עם Promises -- הינם כלי חזר מאוד לתכנות אסינכרוני, כיוון שהם מתווכים -- ואפילו מחסלים לחלוטין -- את הבעיות עם קריאות חוזרות, כגון Callback Hell ו- Inversion of Control.
+ תבנית זו הינה הבסיס לפונקציות async.

+ +

קריאה לפונקצית גנרטור לא מריצה את גוף הפונקציה מיידית; במקום זאת, מוחזר אובייקט איטרטור לפונקציה. כאשר המתודה ()next של האיטרטור נקראת, גוף הפונקציה רץ עד לביטוי ה-{{jsxref("Operators/yield", "yield")}} הראשון, אשר מציין את הערך שיוחזר לאיטרטור, או העברה לפונקציה יוצרת אחרת על-ידי שימוש {{jsxref("Operators/yield*", "*yield")}}. מתודת ה- ()next מחזירה אובייקט עם שדה value, המכיל את הערך המיוצר ושדה done בכעל ערך בוליאני, המציין האם הגנרטור יצר את הערך האחרון. קריאה למתודת ()next עם ארגומנט תמשיך את ריצת פונקציית הגנרטור, תוך החלפת ביטוי ה- yield בו הריצה הופסקה עם הארגומנט מתוך ()next.

+ +

פקודת return בתוך גנרטור, כאשר הוא רץ, תגרום לגנרטור לסיים (כלומר שדה ה- done המוחזר יהיה בעל true). אם ערך מוחזר, הוא יהיה בשדה value של האובייקט המוחזר על-ידי הגנרטור.
+ בדומה לפקודת ה- return, שגיאה שתיזרק בתוך הגנרטור תביא לסיום הגנרטור -- אלא אם היא תיתפס בגוף הגנרטור.
+ כאשר גנרטור מסתיים, קריאות נוספות ל- ()next לא יגרמו לריצה כלשהי של קוד הגנרטור. הן רק יחזירו אובייקט בצורה זו: {value: undefined, done: true}.

+ +

דוגמאות

+ +

דוגמה פשוטה

+ +
function* idMaker() {
+  var index = 0;
+  while (index < index+1)
+    yield index++;
+}
+
+var gen = idMaker();
+
+console.log(gen.next().value); // 0
+console.log(gen.next().value); // 1
+console.log(gen.next().value); // 2
+console.log(gen.next().value); // 3
+// ...
+ +

דוגמה עם *yield

+ +
function* anotherGenerator(i) {
+  yield i + 1;
+  yield i + 2;
+  yield i + 3;
+}
+
+function* generator(i) {
+  yield i;
+  yield* anotherGenerator(i);
+  yield i + 10;
+}
+
+var gen = generator(10);
+
+console.log(gen.next().value); // 10
+console.log(gen.next().value); // 11
+console.log(gen.next().value); // 12
+console.log(gen.next().value); // 13
+console.log(gen.next().value); // 20
+
+ +

העברת ארגומנטים לגנרטורים

+ +
function* logGenerator() {
+  console.log(0);
+  console.log(1, yield);
+  console.log(2, yield);
+  console.log(3, yield);
+}
+
+var gen = logGenerator();
+
+// הקריאה הראשונה ל- next מתבצעת מתחילת הפונקציה
+// עד שהיא מגיעה לפקודת yield הראשונה
+gen.next();             // 0
+gen.next('pretzel');    // 1 pretzel
+gen.next('california'); // 2 california
+gen.next('mayonnaise'); // 3 mayonnaise
+
+ +

פקודת return בתוך גנרטור

+ +
function* yieldAndReturn() {
+  yield "Y";
+  return "R";
+  yield "unreachable";
+}
+
+var gen = yieldAndReturn()
+console.log(gen.next()); // { value: "Y", done: false }
+console.log(gen.next()); // { value: "R", done: true }
+console.log(gen.next()); // { value: undefined, done: true }
+
+ +

לא ניתן ליצור אובייקט גנרטור

+ +
function* f() {}
+var obj = new f; // throws "TypeError: f is not a constructor
+
+ +

גנרטורים המוגדרים בתוך ביטוי

+ +
const foo = function* () {
+  yield 10;
+  yield 20;
+};
+
+const bar = foo();
+console.log(bar.next()); // {value: 10, done: false}
+ +

מפרטים

+ + + + + + + + + + + + + + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('ES2015', '#sec-generator-function-definitions', 'function*')}}{{Spec2('ES2015')}}הגדרה ראשונית.
{{SpecName('ES2016', '#sec-generator-function-definitions', 'function*')}}{{Spec2('ES2016')}}שונה, כך  תהיה מלכודת שבגנרטורים לא [[Construct]] ויזרקו שגיאה בשימוש עם new.
{{SpecName('ESDraft', '#sec-generator-function-definitions', 'function*')}}{{Spec2('ESDraft')}} 
+ +

תאימות לדפדפנים

+ +
+ + +

{{Compat("javascript.statements.generator_function")}}

+
+ +

הערות ספציפיות ל- Firefox

+ +

גנרטורים ואיטרטורים ב- Firefox לפני גרסה 26

+ +

גרסאות Firefox ישנות יותר מממשות הגדרת גנרטורים ישנה יותר. בגרסה הקודמת גנרטורים הוגדרו על-ידי שימוש במילת function רגילה (ללא כוכבית), בין היתר. ראה פונקציה יוצרת מסורתית למידע נוסף.

+ +

אובייקט IteratorResult מוחזר במקום זריקת שגיאה

+ +

החל מ- Gecko 29 {{geckoRelease(29)}}, הפונקציה היוצרת המלאה כבר אינה זורקת {{jsxref("TypeError")}} "generator has already finished". במקום זאת, היא מחזירה אובייקט IteratorResult כדוגמת { value: undefined, done: true } ({{bug(958951)}}).

+ +

ראה גם

+ + diff --git a/files/he/web/javascript/reference/statements/index.html b/files/he/web/javascript/reference/statements/index.html new file mode 100644 index 0000000000..2869ba1ce5 --- /dev/null +++ b/files/he/web/javascript/reference/statements/index.html @@ -0,0 +1,148 @@ +--- +title: Statements and declarations +slug: Web/JavaScript/Reference/Statements +tags: + - JavaScript + - NeedsTranslation + - Reference + - TopicStub + - statements +translation_of: Web/JavaScript/Reference/Statements +--- +
{{jsSidebar("Statements")}}
+ +
יישומי ג'אווה סקריפט מורכבים ממשפטים בעלי תחביר מתאים (syntax).
+ +
משפט אחד יכול להתפרס על גבי מספר שורות.
+ +
מספר משפטים יכולים להופיע על גבי שורה אחת אם בין משפט למשפט מפריד הסמן ";".
+ +
משפט הוא לא מילת מפתח אלא כמה מילות מפתח המאורגנות יחד.
+ +
 
+ +

משפטים והצהרות לפי קטגוריה

+ +

לרשימה אלפבתית ראה סרגל בצד שמאל

+ +

זרימת תהליך

+ +
+
{{jsxref("Statements/block", "Block")}}
+
משפט בלוק נועד לסידור של 0 או יותר משפטים יחד.
+ בלוק תחום בין שני סוגריים מסולסלים.
+
{{jsxref("Statements/break", "break")}}
+
משפט break עוצר את הלולאה, משפט switch או משפט סיווג (label statement) ומעביר את ריצת התוכנה למשפט שמגיע אחרי המשפט שנעצר.
+
{{jsxref("Statements/continue", "continue")}}
+
משפט continue עוצר את האיטרציה הנוכחית\מסווגת של הלולאה הנוכחית וממשיך לאיטרציה הבאה בלולאה.
+
{{jsxref("Statements/Empty", "Empty")}}
+
משפט empty נועד לשימוש כמשפט ריק במקומות בהם התחביר של ג'אווה סקריפט מצפה למשפט.
+
{{jsxref("Statements/if...else", "if...else")}}
+
משפטי if else מריצים משפטים בהתאם לתנאים מסויימים. אם תנאי מסויים לא מתקיים, יכול לרוץ משפט אחר.
+
{{jsxref("Statements/switch", "switch")}}
+
משפט switch מכיל ביטוי מסויים שבהתאם לערך שלו נבחרת פסקת case והמשפטים שבה מורצים.
+
{{jsxref("Statements/throw", "throw")}}
+
משפט throw זורק שגיאה מותאמת אישית.
+
{{jsxref("Statements/try...catch", "try...catch")}}
+
משפט try/catch מנסה להריץ בלוק מסויים, ומגדיר תגובה במקרה שנזרקת שגיאה.
+
+ +

 הצהרות

+ +
+
{{jsxref("Statements/var", "var")}}
+
המילה var מצהירה על משתנה חדש ומאפשרת לתת לו ערך ראשוני.
+
{{experimental_inline}} {{jsxref("Statements/let", "let")}}
+
המילה let מצהירה על משתנה חדש ומקומי לבלוק שבו הוצהר (בניגוד ל var שנמצא בתחום הגלובאלי (global scope), אפשר לתת ערך ראשוני בזמן ההצהרה.
+
{{experimental_inline}} {{jsxref("Statements/const", "const")}}
+
המילה const מצהירה על קבוע (constant) לקריאה בלבד.
+
+ +

פונקציות ומחלקות

+ +
+
{{jsxref("Statements/function", "function")}}
+
המילה function מצהירה על פונקציה עם הפרמטרים שצויינו.
+
{{experimental_inline}} {{jsxref("Statements/function*", "function*")}}
+
פונקציות מחוללות המאפשרות כתיבה פשוטה יותר של איטרטורים.
+
{{jsxref("Statements/return", "return")}}
+
המילה return מציינת את הערך שהפונקציה מחזירה.
+
{{experimental_inline}} {{jsxref("Statements/class", "class")}}
+
המילה class מגדירה מחלקה.
+
+ +

Iterations

+ +
+
{{jsxref("Statements/do...while", "do...while")}}
+
משפט do...while יוצר לולאה שמריצה משפט מסויים עד שהתנאי שבה הופך לשלילי. 
+ בהרצה הראשונה המשפט מורץ בלי קשר לערך התנאי ורק לאחר מכן נבדק לפני כל הרצה מחדש. 
+
{{jsxref("Statements/for", "for")}}
+
משפט for יוצר לולאה שמורכבת משלושה ביטויים (אופציונאליים) התחומים בסוגריים ומופרדים ע"י סמן ";", שלאחריהם מורצים המשפטים שבתוך הלולאה.
+
{{deprecated_inline}} {{non-standard_inline()}} {{jsxref("Statements/for_each...in", "for each...in")}}
+
משפט for each...in עובר על כל הערכים של התכונות באובייקט מסויים ומריץ על כל אחת את המשפט שנמצא בלולאה.
+
{{jsxref("Statements/for...in", "for...in")}}
+
משפט for...in עובר על כל התכונות של אובייקט מסויים, בסדר שרירותי.
+ על תכונה יכולים לרוץ משפטים.
+
{{experimental_inline}} {{jsxref("Statements/for...of", "for...of")}}
+
משפט for...of עובר על אובייקטים הנתנים לאיטרציה (מערכים, אובייקטים, איטרטורים ומחוללים) ויוצר איטרציה מותאמת לכל אחד מהאובייקטים שבה יכולים לרוץ משפטים מסויימים על הערך של כל תכונה באובייקט.
+
{{jsxref("Statements/while", "while")}}
+
משפט while יוצר לולאה שמריצה משפט מסויים כל עוד התנאי שבה חיובי. 
+ התנאי מורץ לפני כל הרצה.
+
+ +

Others

+ +
+
{{jsxref("Statements/debugger", "debugger")}}
+
Invokes any available debugging functionality. If no debugging functionality is available, this statement has no effect.
+
{{experimental_inline}} {{jsxref("Statements/export", "export")}}
+
Used to export functions to make them available for imports in external modules, another scripts.
+
{{experimental_inline}} {{jsxref("Statements/import", "import")}}
+
Used to import functions exported from an external module, another script.
+
{{jsxref("Statements/label", "label")}}
+
Provides a statement with an identifier that you can refer to using a break or continue statement.
+
+ +
+
{{deprecated_inline}} {{jsxref("Statements/with", "with")}}
+
Extends the scope chain for a statement.
+
+ +

Specifications

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('ES6', '#sec-ecmascript-language-statements-and-declarations', 'ECMAScript Language: Statements and Declarations')}}{{Spec2('ES6')}}New: function*, let, for...of, yield, class
{{SpecName('ES5.1', '#sec-12', 'Statements')}}{{Spec2('ES5.1')}} 
{{SpecName('ES3', '#sec-12', 'Statements')}}{{Spec2('ES3')}} 
{{SpecName('ES1', '#sec-12', 'Statements')}}{{Spec2('ES1')}}Initial definition
+ +

See also

+ + diff --git a/files/he/web/javascript/reference/statements/return/index.html b/files/he/web/javascript/reference/statements/return/index.html new file mode 100644 index 0000000000..dea3cb868f --- /dev/null +++ b/files/he/web/javascript/reference/statements/return/index.html @@ -0,0 +1,151 @@ +--- +title: return +slug: Web/JavaScript/Reference/Statements/return +translation_of: Web/JavaScript/Reference/Statements/return +--- +
{{jsSidebar("Statements")}}
+ +

הצהרה באמצעות return מתבצעת בעת סיום הפונקציה, גורמת לעצירתה ומציינת ערך שיש להחזיר למבקש הפונקציה.

+ +
{{EmbedInteractiveExample("pages/js/statement-return.html")}}
+ + + +

תחביר

+ +
return [[expression]]; 
+ +
+
expression
+
הביטוי שערכו יוחזר. במידה והביטוי לא הוזכר, הערך undefined יוחזר במקומו.
+
+ +

תיאור

+ +

השימוש בהצהרה return בגוף הפונקציה, יפסיק את תהליך הריצה שלה.
+ לדוגמה, לפונקציה ()square ארגומנט בשם x, כאשר x יהיה מספר הפונקציה תחזיר את הערך של הארגומנט כפול עצמו.

+ +
function square(x) {
+   return x * x;
+}
+var demo = square(3);
+// demo will equal 9
+
+ +

במידה וארגומנט חסר, הערך שיוחזר יהיה undefined
+ הצהרות ה-return הבאות מפסיקות את הריצה של הפונקציה:

+ +
return;
+return true;
+return false;
+return x;
+return x + y / 3;
+
+ +

הוספת נקודה פסיק באופן אוטומטי

+ +

להצהרה באמצעות return מתווספת באופן אוטומטי נקודה פסיק (ASI).
+ אין לקשר בין שורת ההצהרה של return לביטוי .

+ +
return
+a + b;
+
+ +
return;
+a + b;
+
+ +

כדי להימנע ממצב של הוספת נקודה פסיק באופן אוטומטי, יש להוסיף סוגריים באופן הבא:

+ +
return (
+  a + b
+);
+
+ +

דוגמאות

+ +

עצירת הפונקציה

+ +

הפונקציה נעצרת מיד בעת הקריאה ל-return.

+ +
function counter() {
+  for (var count = 1; ; count++) {  // infinite loop
+    console.log(count + 'A'); // until 5
+      if (count === 5) {
+        return;
+      }
+      console.log(count + 'B');  // until 4
+    }
+  console.log(count + 'C');  // never appears
+}
+
+counter();
+
+// Output:
+// 1A
+// 1B
+// 2A
+// 2B
+// 3A
+// 3B
+// 4A
+// 4B
+// 5A
+
+ +

החזרת פונקציה

+ +

ראו את המאמר הבא בנושא Closures.

+ +
function magic() {
+  return function calc(x) { return x * 42; };
+}
+
+var answer = magic();
+answer(1337); // 56154
+
+ +

מפרט

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('ES1')}}{{Spec2('ES1')}}Initial definition.
{{SpecName('ES5.1', '#sec-12.9', 'Return statement')}}{{Spec2('ES5.1')}} 
{{SpecName('ES6', '#sec-return-statement', 'Return statement')}}{{Spec2('ES6')}} 
{{SpecName('ESDraft', '#sec-return-statement', 'Return statement')}}{{Spec2('ESDraft')}} 
+ +

תאימות דפדפן

+ + + +

{{Compat("javascript.statements.return")}}

+ +

ראו גם

+ + diff --git a/files/he/web/javascript/reference/statements/var/index.html b/files/he/web/javascript/reference/statements/var/index.html new file mode 100644 index 0000000000..64020dee3e --- /dev/null +++ b/files/he/web/javascript/reference/statements/var/index.html @@ -0,0 +1,171 @@ +--- +title: var +slug: Web/JavaScript/Reference/Statements/var +tags: + - hoisted + - הכרזה + - משתנה +translation_of: Web/JavaScript/Reference/Statements/var +--- +
{{jsSidebar("Statements")}}
+ +

מילת ההצהרה var משמשת להכזרה על משתנה.

+ +
{{EmbedInteractiveExample("pages/js/statement-var.html")}}
+ + + +

תחביר

+ +
var varname1 [= value1] [, varname2 [= value2] ... [, varnameN [= valueN]]];
+ +
+
varnameN
+
שם המשתנה.
+
+ +
+
valueN
+
 הערך של המשתנה.
+
+ +

תיאור

+ +

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

+ +

מהם ההבדלים בין משתנים מוצהרים לאלה שאינם?
+ 1. ראשית, משתנים מוצהרים יעשו רק את הפעולה שלשמה נוצרו, לעומת משתנים לא מוצהרים הנחשבים גלובלים.

+ +
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
+
+ +

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

+ +
console.log(a);                // Throws a ReferenceError.
+console.log('still going...'); // Never executes.
+ +
var a;
+console.log(a);                // logs "undefined" or "" depending on browser.
+console.log('still going...'); // logs "still going...".
+ +

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

+ +

var hoisting

+ +

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

+ +
bla = 2;
+var bla;
+// ...
+
+// is implicitly understood as:
+
+var bla;
+bla = 2;
+
+ +

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

+ +

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

+ +
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
+}
+
+ +

דוגמאות

+ +

הכרזה אחת של שני משתנים

+ +
var a = 0, b = 0;
+
+ +

הקצאת שני משתנים עם ערך מחרוזת יחיד

+ +
var a = 'A';
+var b = a;
+
+// Equivalent to:
+
+var a, b = a = 'A';
+
+ +

משתנה מקומי וגלובלי

+ +
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!
+ +

מפרט

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('ES1')}}{{Spec2('ES1')}}Initial definition. Implemented in JavaScript 1.0
{{SpecName('ES5.1', '#sec-12.2', 'var statement')}}{{Spec2('ES5.1')}} 
{{SpecName('ES6', '#sec-variable-statement', 'variable statement')}}{{Spec2('ES6')}} 
{{SpecName('ESDraft', '#sec-variable-statement', 'variable statement')}}{{Spec2('ESDraft')}} 
+ +

תאימות דפדפן

+ + + +

{{Compat("javascript.statements.var")}}

+ +

ראה גם

+ + -- cgit v1.2.3-54-g00ecf