This commit is contained in:
Ilya Kantor 2017-08-03 15:21:40 +02:00
commit 1bb1293de9
4 changed files with 14 additions and 14 deletions

View file

@ -120,7 +120,7 @@ The same thing with an empty string:
alert( '' == false ); // true alert( '' == false ); // true
``` ```
That's because operands of different types are converted to a number by the assignment operator `=`. An empty string, just like `false`, becomes a zero. That's because operands of different types are converted to a number by the equality operator `==`. An empty string, just like `false`, becomes a zero.
What to do if we'd like to differentiate `0` from `false`? What to do if we'd like to differentiate `0` from `false`?

View file

@ -96,7 +96,7 @@ We covered 3 browser-specific functions to interact with the visitor:
: shows a message. : shows a message.
`prompt` `prompt`
: shows a message asking the user to input text. It returns the text or, if CANCEL or `key:Esc` is clicked, all browsers except Safari return `null`. : shows a message asking the user to input text. It returns the text or, if CANCEL or `key:Esc` is clicked, all browsers return `null`.
`confirm` `confirm`
: shows a message and waits for the user to press "OK" or "CANCEL". It returns `true` for OK and `false` for CANCEL/`key:Esc`. : shows a message and waits for the user to press "OK" or "CANCEL". It returns `true` for OK and `false` for CANCEL/`key:Esc`.

View file

@ -1,14 +1,14 @@
# Conditional operators: if, '?' # Conditional operators: if, '?'
Sometimes we need to perform different actions basing on a condition. Sometimes we need to perform different actions based on a condition.
There's an `if` operator for that and also the "question mark" operator: `"?"` for conditional evaluation. There is `if` statement for that and also the conditional (ternary) operator for conditional evaluation which we will be referring as “question mark” operator: `"?"` for simplicity.
[cut] [cut]
## The "if" operator ## The "if" statement
The "if" operator gets a condition, evaluates it and -- if the result is `true` -- executes the code. The "if" statement gets a condition, evaluates it and -- if the result is `true` -- executes the code.
For example: For example:
@ -22,7 +22,7 @@ if (year == 2015) alert( 'You are right!' );
In the example above, the condition is a simple equality check: `year == 2015`, but it can be much more complex. In the example above, the condition is a simple equality check: `year == 2015`, but it can be much more complex.
If there's more than one command to execute -- we can use a code block in figure brackets: If there is more than one command to execute -- we can use a code block in figure brackets:
```js ```js
if (year == 2015) { if (year == 2015) {
@ -31,11 +31,11 @@ if (year == 2015) {
} }
``` ```
It is recommended to use figure brackets every time with `if`, even if there's only one command. That improves readability. It is recommended to use figure brackets every time with `if`, even if there is only one command. That improves readability.
## Boolean conversion ## Boolean conversion
The `if (…)` operator evaluates the expression in parentheses and converts it to the boolean type. The `if (…)` statement evaluates the expression in parentheses and converts it to the boolean type.
Let's recall the conversion rules from the chapter <info:type-conversions>: Let's recall the conversion rules from the chapter <info:type-conversions>:
@ -70,7 +70,7 @@ if (cond) {
## The "else" clause ## The "else" clause
The `if` operator may contain an optional "else" block. It executes when the condition is wrong. The `if` statement may contain an optional "else" block. It executes when the condition is wrong.
For example: For example:
```js run ```js run
@ -85,7 +85,7 @@ if (year == 2015) {
## Several conditions: "else if" ## Several conditions: "else if"
Sometimes we'd like to test several variants of a condition. There's an `else if` clause for that. Sometimes we'd like to test several variants of a condition. There is an `else if` clause for that.
For example: For example:
@ -220,7 +220,7 @@ We don't assign a result to a variable here, the idea is to execute different co
The notation seem to be shorter than `if`, that appeals to some programmers. But it is less readable. The notation seem to be shorter than `if`, that appeals to some programmers. But it is less readable.
Here's the same with `if` for comparison: Here is the same with `if` for comparison:
```js run no-beautify ```js run no-beautify
let company = prompt('Which company created JavaScript?', ''); let company = prompt('Which company created JavaScript?', '');
@ -236,4 +236,4 @@ if (company == 'Netscape') {
Our eyes scan the code vertically. The constructs which span several lines are easier to understand than a long horizontal instruction set. Our eyes scan the code vertically. The constructs which span several lines are easier to understand than a long horizontal instruction set.
The idea of a question mark `'?'` is to return one or another value depending on the condition. Please use it for exactly that. There's `if` to execute different branches of the code. The idea of a question mark `'?'` is to return one or another value depending on the condition. Please use it for exactly that. There is `if` to execute different branches of the code.

View file

@ -182,7 +182,7 @@ Right now they are fully independent.
But we'd want `Rabbit` to extend `Animal`. In other words, rabbits should be based on animals, have access to methods of `Animal` and extend them with its own methods. But we'd want `Rabbit` to extend `Animal`. In other words, rabbits should be based on animals, have access to methods of `Animal` and extend them with its own methods.
What does it mean in the language on prototypes? What does it mean in the language of prototypes?
Right now methods for `rabbit` objects are in `Rabbit.prototype`. We'd like `rabbit` to use `Animal.prototype` as a "fallback", if the method is not found in `Rabbit.prototype`. Right now methods for `rabbit` objects are in `Rabbit.prototype`. We'd like `rabbit` to use `Animal.prototype` as a "fallback", if the method is not found in `Rabbit.prototype`.