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 "Revert "2.3""#88

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

For a long time,JavaScriptevolved without compatibility issues. New features were added to the language while old functionality didn't change.
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.

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.
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.

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"`.
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"`.

##"use strict"
##use strict

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.
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.

For example:
Příklad:

```js
"use strict";

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

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.
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.

````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.
````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.

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

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

"use strict";

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

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

```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.
```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.
```

## Browser console

When you use a [developer console](info:devtools) to run code, please note that it doesn't `use strict` by default.
## Prohlížečová konzole

Sometimes, when `use strict` makes a difference, you'll get incorrect results.
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.

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

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

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

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

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:
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:

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

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

## Should we "use strict"?

The question may sound obvious, but it's not so.
## Měli bychom používat „use strict“?

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

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.
Můžeme vám doporučit, abyste zahajovali skripty s`"use strict"`... ale víte, co je fajn?

**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.**
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.

As of now, we've got to know about `use strict` in general.
**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.**

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.
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.

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

[8]ページ先頭

©2009-2025 Movatter.jp