Skip to content

Commit

Permalink
Merge pull request #210 from alinedmelo/types-ch4-pt3
Browse files Browse the repository at this point in the history
Close #12 - Types & Grammar (Tipos e Gramática) - Capítulo 4 - Finalização de CP
  • Loading branch information
cezaraugusto committed Oct 7, 2018
2 parents 33621f3 + dc133e2 commit 67c2a09
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions types & grammar/ch4.md
Expand Up @@ -506,39 +506,39 @@ Você deve considerar fortemente evitar corção unária com `+` (ou `-`) quando

Lembre-se, nós estamos tentando ser explícitos e **reduzir** a confusão, não torná-la muito pior!

#### `Date` To `number`
#### `Date` para `number`

Another common usage of the unary `+` operator is to coerce a `Date` object into a `number`, because the result is the unix timestamp (milliseconds elapsed since 1 January 1970 00:00:00 UTC) representation of the date/time value:
Outro uso comum do operador unário `+` é converter um objeto `Date` para um `number`, porque o resultado é um valor timestamp unix (milisegundos passados desde 1 de Janeiro 1970 1970 00:00:00 UTC) que é a representação da data/hora.

```js
var d = new Date( "Mon, 18 Aug 2014 08:53:06 CDT" );

+d; // 1408369986000
```

The most common usage of this idiom is to get the current *now* moment as a timestamp, such as:
O uso mais comum dessa representação é pegar o tempo *atual* como uma timestamp, assim:

```js
var timestamp = +new Date();
```

**Note:** Some developers are aware of a peculiar syntactic "trick" in JavaScript, which is that the `()` set on a constructor call (a function called with `new`) is *optional* if there are no arguments to pass. So you may run across the `var timestamp = +new Date;` form. However, not all developers agree that omitting the `()` improves readability, as it's an uncommon syntax exception that only applies to the `new fn()` call form and not the regular `fn()` call form.
**Nota:** Alguns desenvolvedores estão cientes do "truque" sintático peculiar no JavaScript, é que o `()` definido em uma chamada de construtor (uma função chamada com `new`) é *opcional* se não há argumentos para passar. Então você pode se deparar com a forma `var timestamp = +new Date;`. No entanto, nem todos os desenvolvedores concordam que omitir o `()` melhora a legibilidade, pois é uma exceção de sintaxe incomum que se aplica apenas a forma de chamada `new fn()` e não a forma de chamada tradicional `fn()`.

But coercion is not the only way to get the timestamp out of a `Date` object. A noncoercion approach is perhaps even preferable, as it's even more explicit:
Mas coerção não é a única forma de pegar o timestamp de uma objeto `Date`. A abordagem de não coerção talvez seja preferível, já que é mais explícita:

```js
var timestamp = new Date().getTime();
// var timestamp = (new Date()).getTime();
// var timestamp = (new Date).getTime();
```

But an *even more* preferable noncoercion option is to use the ES5 added `Date.now()` static function:
Mas uma opção de não coerção *ainda mais* preferível é usar a função estática adicionada no ES5 `Date.now()`:

```js
var timestamp = Date.now();
```

And if you want to polyfill `Date.now()` into older browsers, it's pretty simple:
E se você quiser fazer o pollyfill de `Date.now()` para navegadores antigos, é bem simples:

```js
if (!Date.now) {
Expand All @@ -548,7 +548,7 @@ if (!Date.now) {
}
```

I'd recommend skipping the coercion forms related to dates. Use `Date.now()` for current *now* timestamps, and `new Date( .. ).getTime()` for getting a timestamp of a specific *non-now* date/time that you need to specify.
Eu recomendaria pular as formas de coerção relacionadas à datas. Use `Date.now()` para timestamps *atuais* e `new Date(..).getTime()` para pergar um timestamp para uma data/hora *não atual* específica que você precise.

#### O curioso caso do `~`

Expand Down

0 comments on commit 67c2a09

Please sign in to comment.