You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
2. ربط طريقة الاعتراض ، على سبيل المثال في المنشئ:
389
379
390
-
```js run
391
-
classButton {
392
-
constructor(value) {
393
-
this.value= value;
394
-
*!*
395
-
this.click=this.click.bind(this);
396
-
*/!*
397
-
}
398
-
399
-
click() {
400
-
alert(this.value);
401
-
}
402
-
}
403
-
404
-
let button =newButton("hello");
405
-
406
-
*!*
407
-
setTimeout(button.click, 1000); // hello
408
-
*/!*
409
-
```
410
-
411
380
توفر حقول class بنية أكثر أناقة للحل الأخير:
412
-
=======
413
-
1. Pass a wrapper-function, such as `setTimeout(() => button.click(), 1000)`.
414
-
2. Bind the method to object, e.g. in the constructor.
415
-
416
-
Class fields provide another, quite elegant syntax:
417
-
>>>>>>> 445bda39806050acd96f87166a7c97533a0c67e9
418
381
419
382
```js run
420
383
classButton {
@@ -433,15 +396,9 @@ let button = new Button("hello");
433
396
setTimeout(button.click, 1000); // hello
434
397
```
435
398
436
-
<<<<<<< HEAD
437
399
ينشئ حقل الفئة `click = () => {...}` وظيفة مستقلة على كل كائن `Button` ، مع` this` مرتبطًا بالكائن. ثم يمكننا تمرير "button.click" في أي مكان ، وسيتم استدعاؤها باستخدام `this` الصحيح.
438
400
439
401
هذا مفيد بشكل خاص في بيئة المتصفح ، عندما نحتاج إلى إعداد طريقة كمستمع للأحداث.
440
-
=======
441
-
The class field `click = () => {...}` is created on a per-object basis, there's a separate function for each `Button` object, with `this` inside it referencing that object. We can pass `button.click` around anywhere, and the value of `this` will always be correct.
442
-
443
-
That's especially useful in browser environment, for event listeners.
0 commit comments