Skip to content

Commit d014c53

Browse files
authored
Merge pull request #209 from otmon76/1.3.6
Polyfills and transpilers
2 parents 2c49349 + a599e0e commit d014c53

File tree

1 file changed

+46
-47
lines changed

1 file changed

+46
-47
lines changed
Lines changed: 46 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,89 +1,88 @@
1+
# Polyfilly a transpilátory
12

2-
# Polyfills and transpilers
3+
Jazyk JavaScript se neustále vyvíjí. Pravidelně se pro tento jazyk objevují nové návrhy, ty jsou analyzovány a jsou-li shledány užitečnými, přidají se na seznam na <https://tc39.github.io/ecma262/> a pak pokračují do [specifikace](https://www.ecma-international.org/publications-and-standards/standards/ecma-262/).
34

4-
The JavaScript language steadily evolves. New proposals to the language appear regularly, they are analyzed and, if considered worthy, are appended to the list at <https://tc39.github.io/ecma262/> and then progress to the [specification](https://www.ecma-international.org/publications-and-standards/standards/ecma-262/).
5+
Týmy vyvíjející JavaScriptové motory mají své vlastní nápady ohledně toho, co implementovat jako první. Mohou se rozhodnout implementovat návrhy, které jsou teprve načrtnuty, a odložit věci, které jsou už ve specifikaci, protože jsou méně zajímavé nebo prostě jen těžší na implementaci.
56

6-
Teams behind JavaScript engines have their own ideas about what to implement first. They may decide to implement proposals that are in draft and postpone things that are already in the spec, because they are less interesting or just harder to do.
7+
Je tedy poměrně běžné, že motor implementuje pouze část standardu.
78

8-
So it's quite common for an engine to implement only part of the standard.
9+
Dobrá stránka, na které uvidíte aktuální stav podpory vlastností jazyka, je <https://compat-table.github.io/compat-table/es6/> (je to velká tabulka, máme ještě hodně co studovat).
910

10-
A good page to see the current state of support for language features is <https://compat-table.github.io/compat-table/es6/> (it's big, we have a lot to study yet).
11+
Jako programátoři rádi používáme nejnovější vlastnosti. Čím více dobrých věcí, tím lépe!
1112

12-
As programmers, we'd like to use most recent features. The more good stuff - the better!
13+
Na druhou stranu, jak přimět náš moderní kód, aby fungoval na starších motorech, které ještě nerozumějí vlastnostem přidaným teprve nedávno?
1314

14-
On the other hand, how to make our modern code work on older engines that don't understand recent features yet?
15+
Jsou dva druhy nástrojů, které to zajistí:
1516

16-
There are two tools for that:
17+
1. Transpilátory.
18+
2. Polyfilly.
1719

18-
1. Transpilers.
19-
2. Polyfills.
20+
V této kapitole je naším cílem získat náhled na to, jak fungují a jaké je jejich místo při vývoji webů.
2021

21-
Here, in this chapter, our purpose is to get the gist of how they work, and their place in web development.
22+
## Transpilátory
2223

23-
## Transpilers
24+
[Transpilátor](https://cs.wikipedia.org/wiki/Transpiler) neboli transpiler je zvláštní druh softwaru, který překládá zdrojový kód do jiného zdrojového kódu. Umí parsovat („přečíst a pochopit“) moderní kód a přepsat jej pomocí starších syntaktických konstrukcí tak, aby kód fungoval i na zastaralých motorech.
2425

25-
A [transpiler](https://en.wikipedia.org/wiki/Source-to-source_compiler) is a special piece of software that translates source code to another source code. It can parse ("read and understand") modern code and rewrite it using older syntax constructs, so that it'll also work in outdated engines.
26+
Například JavaScript před rokem 2020 neměl „operátor koalescence“ `??`. Jestliže tedy návštěvník používá zastaralý prohlížeč, nemusí porozumět kódu `výška = výška ?? 100`.
2627

27-
E.g. JavaScript before year 2020 didn't have the "nullish coalescing operator" `??`. So, if a visitor uses an outdated browser, it may fail to understand the code like `height = height ?? 100`.
28-
29-
A transpiler would analyze our code and rewrite `height ?? 100` into `(height !== undefined && height !== null) ? height : 100`.
28+
Transpilátor analyzuje náš kód a přepíše `výška ?? 100` na `(výška !== undefined && výška !== null) ? výška : 100`.
3029

3130
```js
32-
// before running the transpiler
33-
height = height ?? 100;
31+
// před spuštěním transpilátoru
32+
výška = výška ?? 100;
3433

35-
// after running the transpiler
36-
height = (height !== undefined && height !== null) ? height : 100;
34+
// po spuštění transpilátoru
35+
výška = (výška !== undefined && výška !== null) ? výška : 100;
3736
```
3837
39-
Now the rewritten code is suitable for older JavaScript engines.
38+
Nyní je přepsaný kód vhodný i pro starší JavaScriptové motory.
4039
41-
Usually, a developer runs the transpiler on their own computer, and then deploys the transpiled code to the server.
40+
Vývojář si obvykle spustí transpilátor na svém vlastním počítači a pak zveřejní transpilovaný kód na serveru.
4241
43-
Speaking of names, [Babel](https://babeljs.io) is one of the most prominent transpilers out there.
42+
Když hovoříme o názvech, jedním z nejvýznamnějších transpilátorů je [Babel](https://babeljs.io).
4443
45-
Modern project build systems, such as [webpack](https://webpack.js.org/), provide a means to run a transpiler automatically on every code change, so it's very easy to integrate into the development process.
44+
Moderní systémy pro vytváření projektů, například [webpack](https://webpack.js.org/), poskytují způsoby, jak spouštět transpilátor automaticky při každé změně kódu, takže je velmi jednoduché jej integrovat do procesu vývoje.
4645
47-
## Polyfills
46+
## Polyfilly
4847
49-
New language features may include not only syntax constructs and operators, but also built-in functions.
48+
Mezi nové vlastnosti jazyka mohou patřit nejenom syntaktické konstrukce a operátory, ale také vestavěné funkce.
5049
51-
For example, `Math.trunc(n)` is a function that "cuts off" the decimal part of a number, e.g `Math.trunc(1.23)` returns `1`.
50+
Například `Math.trunc(n)` je funkce, která „odřízne“ desetinnou část čísla, např. `Math.trunc(1.23)` vrátí `1`.
5251
53-
In some (very outdated) JavaScript engines, there's no `Math.trunc`, so such code will fail.
52+
V některých (velmi zastaralých) JavaScriptových motorech není funkce `Math.trunc`, takže takový kód selže.
5453
55-
As we're talking about new functions, not syntax changes, there's no need to transpile anything here. We just need to declare the missing function.
54+
Protože hovoříme o nových funkcích a ne o syntaktických změnách, není tady potřeba nic transpilovat. Potřebujeme jenom deklarovat chybějící funkci.
5655
57-
A script that updates/adds new functions is called "polyfill". It "fills in" the gap and adds missing implementations.
56+
Skript, který vylepšuje nebo přidává nové funkce, se nazývá „polyfill“. „Vyplní“ (anglicky „fill in“) mezeru a přidá chybějící implementace.
5857
59-
For this particular case, the polyfill for `Math.trunc` is a script that implements it, like this:
58+
V tomto konkrétním případě je polyfill pro `Math.trunc` skript, který ji implementuje, například takto:
6059
6160
```js
62-
if (!Math.trunc) { // if no such function
63-
// implement it
64-
Math.trunc = function(number) {
65-
// Math.ceil and Math.floor exist even in ancient JavaScript engines
66-
// they are covered later in the tutorial
67-
return number < 0 ? Math.ceil(number) : Math.floor(number);
61+
if (!Math.trunc) { // není-li taková funkce
62+
// implementuje ji
63+
Math.trunc = function(číslo) {
64+
// Math.ceil a Math.floor existují i v nejstarších JavaScriptových motorech
65+
// budou vysvětleny později v tomto tutoriálu
66+
return číslo < 0 ? Math.ceil(číslo) : Math.floor(číslo);
6867
};
6968
}
7069
```
7170
72-
JavaScript is a highly dynamic language. Scripts may add/modify any function, even built-in ones.
71+
JavaScript je vysoce dynamický jazyk. Skripty mohou přidávat nebo modifikovat libovolné funkce, dokonce i vestavěné.
7372
74-
One interesting polyfill library is [core-js](https://github.com/zloirock/core-js), which supports a wide range of features and allows you to include only the ones you need.
73+
Zajímavá knihovna polyfillů je [core js](https://github.com/zloirock/core-js), která podporuje širokou škálu vlastností a umožňuje vám přidat jen ty, které potřebujete.
7574
76-
## Summary
75+
## Shrnutí
7776
78-
In this chapter we'd like to motivate you to study modern and even "bleeding-edge" language features, even if they aren't yet well-supported by JavaScript engines.
77+
V této kapitole jsme vás chtěli motivovat k prostudování moderních vlastností jazyka, včetně těch „za hranou“, i když ještě nejsou příliš podporovány v motorech JavaScriptu.
7978
80-
Just don't forget to use a transpiler (if using modern syntax or operators) and polyfills (to add functions that may be missing). They'll ensure that the code works.
79+
Jen nezapomínejte používat transpilátor (používáte-li moderní syntaxi nebo operátory) a polyfilly (abyste přidali funkce, které mohou chybět). Ty zajistí, že váš kód bude fungovat.
8180
82-
For example, later when you're familiar with JavaScript, you can setup a code build system based on [webpack](https://webpack.js.org/) with the [babel-loader](https://github.com/babel/babel-loader) plugin.
81+
Například později, až budete JavaScript dobře znát, si můžete nastavit systém pro vytváření kódu založený na [webpacku](https://webpack.js.org/) s pluginem [babel-loader](https://github.com/babel/babel-loader).
8382
84-
Good resources that show the current state of support for various features:
85-
- <https://compat-table.github.io/compat-table/es6/> - for pure JavaScript.
86-
- <https://caniuse.com/> - for browser-related functions.
83+
Dobré zdroje, které ukazují aktuální stav podpory různých vlastností:
84+
- <https://kangax.github.io/compat-table/es6/> - pro čistý JavaScript.
85+
- <https://caniuse.com/> - pro funkce vztahující se k prohlížeči.
8786
88-
P.S. Google Chrome is usually the most up-to-date with language features, try it if a tutorial demo fails. Most tutorial demos work with any modern browser though.
8987
88+
P.S. Pokud se týká vlastností jazyka, obvykle je nejaktuálnější Google Chrome. Pokud vám některé demo v tomto tutoriálu selže, zkuste ho. Většina dem v tutoriálu však funguje na kterémkoli moderním prohlížeči.

0 commit comments

Comments
 (0)