Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Revert "2.3"#85

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
otmon76 merged 1 commit intomasterfromrevert-68-2.3
May 1, 2022
Merged
Changes fromall 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
78 changes: 41 additions & 37 deletions1-js/02-first-steps/03-strict-mode/article.md
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,85 +1,89 @@
#Moderní režim, „use strict
#The modern mode, "use strict"

Dlouhou dobu bylJavaScriptvyvíjen bez problémů s kompatibilitou. Do jazyka byly přidávány nové prvky, zatímco stará funkcionalita se neměnila.
For a long time,JavaScriptevolved without compatibility issues. New features were added to the language while old functionality didn't change.

Mělo to tu výhodu, že se nerozbíjel již existující kód. Nevýhodou však bylo, že každá chyba nebo nedokonalé rozhodnutí tvůrců JavaScriptu zůstala v jazyce zakotvena navždy.
That had the benefit of never breaking existing code. But the downside was that any mistake or an imperfect decision made by JavaScript's creators got stuck in the language forever.

To platilo až do roku 2009, kdy se objevilECMAScript 5 (ES5), který do jazyka přidal nové vlastnosti a upravil některé již existující. Aby starý kód nepřestal fungovat, většina těchto úprav je defaultně vypnuta. Musíte je výslovně povolit speciální direktivou: `"use strict"`.
This was the case until 2009 whenECMAScript 5 (ES5) appeared. It added new features to the language and modified some of the existing ones. To keep the old code working, most such modifications are off by default. You need to explicitly enable them with a special directive: `"use strict"`.

##use strict
##"use strict"

Tato direktiva má podobu řetězce: `"use strict"`nebo `'use strict'`.Když je umístěna na začátku skriptu, celý skript bude fungovat „moderním“ způsobem.
The directive looks like a string: `"use strict"`or `'use strict'`.When it is located at the top of a script, the whole script works the "modern" way.

Příklad:
For example:

```js
"use strict";

//tento kód funguje moderním způsobem
//this code works the modern way
...
```

Brzy se naučíme používat funkce (způsob, jak seskupit příkazy),a tak s předstihem zmíníme, že`"use strict"`můžeme umístit i na začátek funkce. Když to uděláme, umožníme striktní režim pouze v této funkci. Většinou jej však lidé používají pro celý skript.
Quite soon we're going to learn functions (a way to group commands),so let's note in advance that`"use strict"`can be put at the beginning of a function. Doing that enables strict mode in that function only. But usually people use it for the whole script.

````warn header="Zajistěte, aby „use strict“ bylo na začátku"
Zajistěte, aby`"use strict"`bylo hned na samém začátku vašeho skriptu, jinak nebude striktní režim povolen.
````warn header="Ensure that \"use strict\" is at the top"
Please make sure that`"use strict"`is at the top of your scripts, otherwise strict mode may not be enabled.

Zde není striktní režim povolen:
Strict mode isn't enabled here:

```js no-strict
alert("nějaký kód");
//níže uvedený"use strict"se ignoruje -- musí být na začátku
alert("some code");
// "use strict"below is ignored--it must be at the top

"use strict";

//striktní režim není aktivován
//strict mode is not activated
```

Nad`"use strict"` nesmí být nic jiného než komentáře.
Only comments may appear above`"use strict"`.
````

```warn header="`use strict` nelze nijak zrušit"
Neexistuje žádná direktiva jako `"no use strict"`, která by vrátila motoru výchozí chování. Jakmile jednou vstoupíme do striktního režimu, už není cesty zpět.
```warn header="There's no way to cancel `use strict`"
There is no directive like `"no use strict"` that reverts the engine to old behavior.

Once we enter strict mode, there's no going back.
```

## Prohlížečová konzole
## Browser console

When you use a [developer console](info:devtools) to run code, please note that it doesn't `use strict` by default.

Když spustíte kód ve [vývojářské konzoli](info:devtools), neběží automaticky ve striktním režimu.
Někdy to může ovlivnit i samotný výsledek.
Sometimes, when `use strict` makes a difference, you'll get incorrect results.

Jak tedy vlastně používat `use strict`v konzoli?
So, how to actually `use strict`in the console?

Můžete zkusit vložit více řádků pomocí zkratky`key:Shift+Enter`a umístit`use strict`na začátek, například takto:
First, you can try to press`key:Shift+Enter`to input multiple lines, and put`use strict`on top, like this:

```js
'use strict'; <Shift+Enterpro nový řádek>
// ...váš kód
<Enterpro spuštění>
'use strict'; <Shift+Enterfor a newline>
// ...your code
<Enterto run>
```

Funguje to ve většině prohlížečů, konkrétně ve Firefoxu a Chrome.
It works in most browsers, namely Firefox and Chrome.

Pokud to nefunguje, např. v nějakém starém prohlížeči, existuje jeden nepěkný, ale spolehlivý způsob, jak zajistit `use strict`.Umístěte jej do tohoto obalu:
If it doesn't, e.g. in an old browser, there's an ugly, but reliable way to ensure `use strict`.Put it inside this kind of wrapper:

```js
(function() {
'use strict';

// ...zde je váš kód...
// ...your code here...
})()
```

## Měli bychom používat „use strict“?
## Should we "use strict"?

The question may sound obvious, but it's not so.

Odpověď na tuto otázku se může zdát samozřejmá, ale není tomu tak.
One could recommend to start scripts with `"use strict"`... But you know what's cool?

Můžeme vám doporučit, abyste zahajovali skripty s`"use strict"`... ale víte, co je fajn?
Modern JavaScript supports "classes" and "modules" - advanced language structures (we'll surely get to them), that enable `use strict` automatically. So we don't need to add the`"use strict"` directive, if we use them.

Moderní JavaScript podporuje „třídy“ a „moduly“ – pokročilé jazykové struktury (určitě se k nim dostaneme), které používají `use strict` automaticky. Pokud je tedy používáme, nemusíme direktivu `use strict` přidávat.
**So, for now `"use strict";` is a welcome guest at the top of your scripts. Later, when your code is all in classes and modules, you may omit it.**

**Prozatím tedy `"use strict"` používejte; na začátku vašich skriptů bývá vítaným hostem. Později, až budete mít celý svůj kód v třídách a modulech, jej můžete vypustit.**
As of now, we've got to know about `use strict` in general.

Prozatím tedy víme, jak obecně `use strict` používat.
Až se v dalších kapitolách naučíme další vlastnosti jazyka, poznáme rozdíly mezi striktním a starším režimem. Naštěstí jich není mnoho a ve skutečnosti nám spíše ulehčují práci.
In the next chapters, as we learn language features, we'll see the differences between the strict and old modes. Luckily, there aren't many and they actually make our lives better.

Všechny příklady v našem tutoriálu předpokládají striktní režim, pokud není (velmi zřídka) uvedeno jinak.
All examples in this tutorial assume strict mode unless (very rarely) specified otherwise.

[8]ページ先頭

©2009-2025 Movatter.jp