Skip to content

JSON methods, toJSON #24

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions 1-js/05-data-types/12-json/1-serialize-object/task.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
importance: 5
درجة الأهمية: 5

---

# Turn the object into JSON and back
# تحويل الكائن لجيسون وإرجاعه مرة أخرى

Turn the `user` into JSON and then read it back into another variable.
قم بتحويل الكائن `user` إلى JSON ثم قم بإضافته ككائن إلى متغير آخر.

```js
let user = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@ alert( JSON.stringify(meetup, function replacer(key, value) {
*/
```

Here we also need to test `key==""` to exclude the first call where it is normal that `value` is `meetup`.
نحتاج أن نفحص أيضًا `key==""` لاستثناء أول استدعاء لأن قيمته الطبيعية هي الكائن `meetup`.

14 changes: 7 additions & 7 deletions 1-js/05-data-types/12-json/2-serialize-event-circular/task.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
importance: 5
درجة الأهمية: 5

---

# Exclude backreferences
# استثناء المراجع لكائنات أخرى

In simple cases of circular references, we can exclude an offending property from serialization by its name.
فى بعض الحالات البسيطة من المرجعية الثنائية (circular references)، يمكننا أن نستبعد الخاصية الغير مرغوبة من التحويل عن طريق اسمها.

But sometimes we can't just use the name, as it may be used both in circular references and normal properties. So we can check the property by its value.
ولكن فى بعض الأوقات لا يمكننا أن نستخدم الإسم فقط، حيث أنها يمكن أن تكون مستخدمه فى مرجعية ثنائية وفى خاصية عادية، ولذلك يمكننا أن نختبر الخاصية عن طريق اسمها.

Write `replacer` function to stringify everything, but remove properties that reference `meetup`:
قم بإنشاء الدالة `replacer` لتحويل كل شيئ إلى نص ولكن تقوم بحذف الخصائص التى تحتوى على مرجع للكائن `meetup`:

```js run
let room = {
Expand All @@ -28,10 +28,10 @@ meetup.self = meetup;
*/!*

alert( JSON.stringify(meetup, function replacer(key, value) {
/* your code */
/* الحل الخاص بك */
}));

/* result should be:
/* يجب أن تكون النتيجة كالآتي:
{
"title":"Conference",
"occupiedBy":[{"name":"John"},{"name":"Alice"}],
Expand Down
275 changes: 141 additions & 134 deletions 1-js/05-data-types/12-json/article.md

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ function f() {
alert("Hello!");
}

f.defer(1000); // shows "Hello!" after 1 sec
f.defer(1000); // تعرض كلمة "Hello!" بعد 1 ثانية
```
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
importance: 5
درجة الأهمية: 5

---

# Add method "f.defer(ms)" to functions
# إضافة الدالة "f.defer(ms)" للدوال

Add to the prototype of all functions the method `defer(ms)`, that runs the function after `ms` milliseconds.
أضف إلى نموذج الدوال الدالة `defer(ms)` والتى تقوم بتشغيل الدالة بعد `ms` مللى ثانية.

After you do it, such code should work:
بعد أن تفعلها، سيعمل الكود التالى:

```js
function f() {
alert("Hello!");
}

f.defer(1000); // shows "Hello!" after 1 second
f.defer(1000); // تعرض كلمة "Hello!" بعد 1 ثانية
```
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@ Function.prototype.defer = function(ms) {
}
};

// check it
// اختبر
function f(a, b) {
alert( a + b );
}

f.defer(1000)(1, 2); // shows 3 after 1 sec
f.defer(1000)(1, 2); // تعرض 3 بعد ثانية واحدة
```

Please note: we use `this` in `f.apply` to make our decoration work for object methods.
لاحظ: استخدمنا `this` فى `f.apply` لتعمل مع دوال الكائنات.

So if the wrapper function is called as an object method, then `this` is passed to the original method `f`.
ولذلك إذا تم استدعاء دالة كدالة كائن (method) فإن `this` سيتم تمرريرها إلى الدالة الأصلية `f`.

```js run
Function.prototype.defer = function(ms) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
importance: 4
درجة الأهمية: 4

---

# Add the decorating "defer()" to functions
# إضافة الدالة المعدلة للدوال

Add to the prototype of all functions the method `defer(ms)`, that returns a wrapper, delaying the call by `ms` milliseconds.
أضف إلى النموذج الخاص بالدوال الدالة `defer(ms)`، والتى تقوم بإرجاع حاوى (wrapper) وتؤخر التنفيذ بعد `ms` مللى ثانية.

Here's an example of how it should work:
هاك مثال لكيفية استخدامها:

```js
function f(a, b) {
alert( a + b );
}

f.defer(1000)(1, 2); // shows 3 after 1 second
f.defer(1000)(1, 2); // تعرض 3 بعد ثانية واحدة
```

Please note that the arguments should be passed to the original function.
لاحظ أن المتغيرات يجب أن تُمرر إلى الدالة الأصلية.
Loading