Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [JavaScript]if, uproszczenie kodu
stellatus
post 21.06.2020, 22:57:50
Post #1





Grupa: Zarejestrowani
Postów: 196
Pomógł: 0
Dołączył: 9.03.2017

Ostrzeżenie: (0%)
-----


Kod
window.onkeydown = function keyboardShortcuts(e) {
    if (e.keyCode == 37 || e.keyCode == 38) {
        galleryTop.slidePrev()
        sidenavCloseIfOpen()
    }
    if (e.keyCode == 39 || e.keyCode == 40 || e.keyCode == 32) {
        galleryTop.slideNext()
        sidenavCloseIfOpen()
    }
    if (e.keyCode == 65) {
        sidenavCloseIfOpen()
        autoplayToggle()
    }
    if (e.keyCode == 84) {
        if (thumbnailsToggle.checked) {
            thumbnailsToggle.checked = false
        } else {
            thumbnailsToggle.checked = true
        }
        thumbnailsSwitcher()
    }
    if (e.keyCode == 70) {
        fullscreenToggle()
    }
    if (e.keyCode == 73) {
        iconBarToggle()
    }
    if (e.keyCode == 77) {
        sidenavToggle()
    }
}


Bardzo dużo tutaj powtórzeń. Da się to jakoś prościej zapisać?

Ten post edytował stellatus 21.06.2020, 23:07:09
Go to the top of the page
+Quote Post
trueblue
post 22.06.2020, 08:14:04
Post #2





Grupa: Zarejestrowani
Postów: 6 761
Pomógł: 1822
Dołączył: 11.03.2014

Ostrzeżenie: (0%)
-----


  1. if (thumbnailsToggle.checked) {
  2. thumbnailsToggle.checked = false
  3. } else {
  4. thumbnailsToggle.checked = true
  5. }


  1. thumbnailsToggle.checked = !thumbnailsToggle.checked;


if możesz zastąpić switch. I tyle.


--------------------
Go to the top of the page
+Quote Post
stellatus
post 23.06.2020, 09:38:13
Post #3





Grupa: Zarejestrowani
Postów: 196
Pomógł: 0
Dołączył: 9.03.2017

Ostrzeżenie: (0%)
-----


Wielkie dzięki! O to właśnie mi chodziło:

Kod
window.onkeydown = function keyboardShortcuts(e) {
    switch (e.keyCode) {
        case 37:
        case 38:
            galleryTop.slidePrev()
            sidenavCloseIfOpen()
            break
        case 39:
        case 40:
        case 32:
            galleryTop.slideNext()
            sidenavCloseIfOpen()
            break
        case 65:
            sidenavCloseIfOpen()
            autoplayToggle()
            break
        case 84:
            if (thumbnailsToggle.checked) {
                thumbnailsToggle.checked = false
            } else {
                thumbnailsToggle.checked = true
            }
            thumbnailsSwitcher()
            break
        case 70:
            fullscreenToggle()
            break
        case 73:
            iconBarToggle()
            break
        case 77:
            sidenavToggle()
            break
    }
}


Działa znakomicie. Tylko nie rozumiem tego:
Kod
thumbnailsToggle.checked = !thumbnailsToggle.checked
Go to the top of the page
+Quote Post
nospor
post 23.06.2020, 09:40:29
Post #4





Grupa: Moderatorzy
Postów: 36 455
Pomógł: 6292
Dołączył: 27.12.2004




! - to negacja

!false = true
!true = false


--------------------

"Myśl, myśl, myśl..." - Kubuś Puchatek || "Manual, manual, manual..." - Kubuś Programista
"Szukaj, szukaj, szukaj..." - Kubuś Odkrywca || "Debuguj, debuguj, debuguj..." - Kubuś Developer

Go to the top of the page
+Quote Post
stellatus
post 23.06.2020, 09:59:39
Post #5





Grupa: Zarejestrowani
Postów: 196
Pomógł: 0
Dołączył: 9.03.2017

Ostrzeżenie: (0%)
-----


To wiem, ale nie rozumiem jak to ma mi pomóc w uproszczenu kodu.
Go to the top of the page
+Quote Post
nospor
post 23.06.2020, 10:02:50
Post #6





Grupa: Moderatorzy
Postów: 36 455
Pomógł: 6292
Dołączył: 27.12.2004




No zamiast 5 linijek
if (thumbnailsToggle.checked) {
thumbnailsToggle.checked = false
} else {
thumbnailsToggle.checked = true
}

Masz tylko jedna
thumbnailsToggle.checked = !thumbnailsToggle.checked

To sie nazywa uproszczenie kodu


--------------------

"Myśl, myśl, myśl..." - Kubuś Puchatek || "Manual, manual, manual..." - Kubuś Programista
"Szukaj, szukaj, szukaj..." - Kubuś Odkrywca || "Debuguj, debuguj, debuguj..." - Kubuś Developer

Go to the top of the page
+Quote Post
stellatus
post 23.06.2020, 10:16:33
Post #7





Grupa: Zarejestrowani
Postów: 196
Pomógł: 0
Dołączył: 9.03.2017

Ostrzeżenie: (0%)
-----


Wielkie dzięki! W ogóle tego nawet na początku nie wpisywałem, bo z góry założyłem, że nie zadziała. Jednak działa, ale nie mam pojęcia w jaki sposób. Muszę to przeanalizować.
Go to the top of the page
+Quote Post
nospor
post 23.06.2020, 10:19:49
Post #8





Grupa: Moderatorzy
Postów: 36 455
Pomógł: 6292
Dołączył: 27.12.2004




thumbnailsToggle.checked = false
to rozumiesz? do thumbnailsToggle.checked przypisujesz false.

Na takiej samej zasadzie dziala
thumbnailsToggle.checked = !thumbnailsToggle.checked
do thumbnailsToggle.checked przypisujesz negacje thumbnailsToggle.checked. czyli jak bylo false to bedzie true. Jak bylo true to bedzie false.


--------------------

"Myśl, myśl, myśl..." - Kubuś Puchatek || "Manual, manual, manual..." - Kubuś Programista
"Szukaj, szukaj, szukaj..." - Kubuś Odkrywca || "Debuguj, debuguj, debuguj..." - Kubuś Developer

Go to the top of the page
+Quote Post
stellatus
post 23.06.2020, 10:26:22
Post #9





Grupa: Zarejestrowani
Postów: 196
Pomógł: 0
Dołączył: 9.03.2017

Ostrzeżenie: (0%)
-----


Tak, teraz rozumiem. To jest piękne.
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Wersja Lo-Fi Aktualny czas: 24.04.2024 - 14:49