|
517 | 517 |
|
518 | 518 | B. Coerced Types
|
519 | 519 |
|
520 |
| - Consider the implications of the following... |
| 520 | + Σκεφτείτε τις εφαρμογές των παρακάτω... |
521 | 521 |
|
522 |
| - Given this HTML: |
| 522 | + Έχοντας αυτό το HTML: |
523 | 523 |
|
524 | 524 | ```html
|
525 | 525 |
|
|
532 | 532 |
|
533 | 533 | // 3.B.1.1
|
534 | 534 |
|
535 |
| - // `foo` has been declared with the value `0` and its type is `number` |
| 535 | + // `foo` έχει γίνει declared με την τιμή `0` και ο τύπος είναι `number` |
536 | 536 | var foo = 0;
|
537 | 537 |
|
538 | 538 | // typeof foo;
|
539 | 539 | // "number"
|
540 | 540 | ...
|
541 | 541 |
|
542 |
| - // Somewhere later in your code, you need to update `foo` |
543 |
| - // with a new value derived from an input element |
| 542 | + // Κάπου αργότερα στον κώδικα, πρέπει να ανανεώσετε το `foo` |
| 543 | + // με μία καινούρια τιμή από ένα input element |
544 | 544 |
|
545 | 545 | foo = document.getElementById("foo-input").value;
|
546 | 546 |
|
547 |
| - // If you were to test `typeof foo` now, the result would be `string` |
548 |
| - // This means that if you had logic that tested `foo` like: |
| 547 | + // Άμα θέλατε να τεστάρετε `typeof foo` τώρα, το αποτέλεσμα θα ήταν `string` |
| 548 | + // Αυτό σημαίνει πως αν είχατε λογική που να τεστάρει το `foo`: |
549 | 549 |
|
550 | 550 | if ( foo === 1 ) {
|
551 | 551 |
|
552 | 552 | importantTask();
|
553 | 553 |
|
554 | 554 | }
|
555 | 555 |
|
556 |
| - // `importantTask()` would never be evaluated, even though `foo` has a value of "1" |
| 556 | + // `importantTask()` δεν θα γινόταν ποτέ evaluated, ακόμα και αν το `foo` έχει την τιμή "1" |
557 | 557 |
|
558 | 558 |
|
559 | 559 | // 3.B.1.2
|
560 | 560 |
|
561 |
| - // You can preempt issues by using smart coercion with unary + or - operators: |
| 561 | + // Μπορείτε να προκατέχετε τέτοια προβλήματα χρησιμοποιώντας smart coercion με unary + ή - operators: |
562 | 562 |
|
563 | 563 | foo = +document.getElementById("foo-input").value;
|
564 |
| - // ^ unary + operator will convert its right side operand to a number |
| 564 | + // ^ unary + operator θα μετατρέψουν το δεξιά operand σε αριθμό |
565 | 565 |
|
566 | 566 | // typeof foo;
|
567 | 567 | // "number"
|
|
572 | 572 |
|
573 | 573 | }
|
574 | 574 |
|
575 |
| - // `importantTask()` will be called |
| 575 | + // `importantTask()` θα κληθεί |
576 | 576 | ```
|
577 | 577 |
|
578 |
| - Here are some common cases along with coercions: |
| 578 | + Παρακάτω είναι μερικές συχνές περιπτώσεις με coercions: |
579 | 579 |
|
580 | 580 |
|
581 | 581 | ```javascript
|
|
661 | 661 | !!~array.indexOf("d");
|
662 | 662 | // false
|
663 | 663 |
|
664 |
| - // Note that the above should be considered "unnecessarily clever" |
665 |
| - // Prefer the obvious approach of comparing the returned value of |
666 |
| - // indexOf, like: |
| 664 | + // Παρατηρείστε πως τα παραπάνω πρέπει να θεωρούνται "unnecessarily clever" |
| 665 | + // Να προτιμάτε την φανερή προσέγγιση συγκρίνοντας την επιστρεφόμενη τιμή του |
| 666 | + // indexOf, όπως: |
667 | 667 |
|
668 | 668 | if ( array.indexOf( "a" ) >= 0 ) {
|
669 | 669 | // ...
|
|
678 | 678 |
|
679 | 679 | parseInt( num, 10 );
|
680 | 680 |
|
681 |
| - // is the same as... |
| 681 | + // είναι το ίδιο με... |
682 | 682 |
|
683 | 683 | ~~num;
|
684 | 684 |
|
685 | 685 | num >> 0;
|
686 | 686 |
|
687 | 687 | num >>> 0;
|
688 | 688 |
|
689 |
| - // All result in 2 |
| 689 | + // Όλα επιστρέφουν 2 |
690 | 690 |
|
691 | 691 |
|
692 |
| - // Keep in mind however, that negative numbers will be treated differently... |
| 692 | + // Να θυμάστε βέβαια, πως οι αρνητικοί αριθμοί θα αντιμετωπιστούν διαφορετικά... |
693 | 693 |
|
694 | 694 | var neg = -2.5;
|
695 | 695 |
|
696 | 696 | parseInt( neg, 10 );
|
697 | 697 |
|
698 |
| - // is the same as... |
| 698 | + // είναι το ίδιο με... |
699 | 699 |
|
700 | 700 | ~~neg;
|
701 | 701 |
|
702 | 702 | neg >> 0;
|
703 | 703 |
|
704 |
| - // All result in -2 |
705 |
| - // However... |
| 704 | + // Όλα επιστρέφουν -2 |
| 705 | + // Όμως... |
706 | 706 |
|
707 | 707 | neg >>> 0;
|
708 | 708 |
|
709 |
| - // Will result in 4294967294 |
| 709 | + // Θα επιστρέψει 4294967294 |
710 | 710 |
|
711 | 711 |
|
712 | 712 |
|
|
0 commit comments