This commit is contained in:
Ilya Kantor 2017-08-09 23:34:30 +02:00
commit e43e1a0952
7 changed files with 13 additions and 12 deletions

View file

@ -1,4 +1,4 @@
# An introduction to JavaScript
# An Introduction to JavaScript
Let's see what's so special about JavaScript, what we can achieve with it and which other technologies play well with it.

View file

@ -201,7 +201,7 @@ Funny code, isn't it? We should understand how it works, because sometimes we ca
## Remainder %
The remainder operator `%` despite it's look does not have a relation to percents.
The remainder operator `%` despite its look does not have a relation to percents.
The result of `a % b` is the remainder of the integer division of `a` by `b`.
@ -415,7 +415,7 @@ let a = (1+2, 3+4);
alert( a ); // 7 (the result of 3+4)
```
Here, the first expression `1+2` is evaluated, and it's result is thrown away, then `3+4` is evaluated and returned as the result.
Here, the first expression `1+2` is evaluated, and its result is thrown away, then `3+4` is evaluated and returned as the result.
```smart header="Comma has a very low precedence"
Please note that the comma operator has very low precedence, lower than `=`, so parentheses are important in the example above.

View file

@ -155,6 +155,7 @@ switch (arg) {
case '0':
case '1':
alert( 'One or zero' );
break;
case '2':
alert( 'Two' );
@ -162,7 +163,7 @@ switch (arg) {
case 3:
alert( 'Never executes!' );
break;
default:
alert( 'An unknown value' )
}

View file

@ -316,7 +316,7 @@ The result of a property access `user.hi` is not a function, but a value of Refe
When parentheses `()` are called on the Reference Type, they receive the full information about the object and it's method, and can set the right `this` (`=user` in this case).
Any other operation like assignment `hi = user.hi` discards the reference type as a whole, takes the value of `user.hi` (a function) and passes it on. So any further operation "looses" `this`.
Any other operation like assignment `hi = user.hi` discards the reference type as a whole, takes the value of `user.hi` (a function) and passes it on. So any further operation "loses" `this`.
So, as the result, the value of `this` is only passed the right way if the function is called directly using a dot `obj.method()` or square brackets `obj[method]()` syntax (they do the same here).

View file

@ -1,7 +1,7 @@
# Object to primitive conversion
What happens when objects are added `obj1 + obj2`, substracted `obj1 - obj2` or printed using `alert(obj)`?
What happens when objects are added `obj1 + obj2`, subtracted `obj1 - obj2` or printed using `alert(obj)`?
There are special methods in objects that do the conversion.
@ -11,7 +11,7 @@ In the chapter <info:type-conversions> we've seen the rules for numeric, string
For objects, there's no to-boolean conversion, because all objects are `true` in a boolean context. So there are only string and numeric conversions.
The numeric conversion happens when we substract objects or apply mathematical functions. For instance, `Date` objects (to be covered in the chapter <info:date>) can be substracted, and the result of `date1 - date2` is the time difference between two dates.
The numeric conversion happens when we subtract objects or apply mathematical functions. For instance, `Date` objects (to be covered in the chapter <info:date>) can be subtracted, and the result of `date1 - date2` is the time difference between two dates.
As for the string conversion -- it usually happens when we output an object like `alert(obj)` and in similar contexts.

View file

@ -162,16 +162,16 @@ alert( new SmallUser().name ); // John
Usually constructors don't have a `return` statement. Here we mention the special behavior with returning objects mainly for the sake of completeness.
````smart header="Omitting brackets"
By the way, we can omit brackets after `new`, if it has no arguments:
````smart header="Omitting parentheses"
By the way, we can omit parentheses after `new`, if it has no arguments:
```js
let user = new User; // <-- no brackets
let user = new User; // <-- no parentheses
// same as
let user = new User();
```
Omitting brackets here is not considered a "good style", but the syntax is permitted by specification.
Omitting parentheses here is not considered a "good style", but the syntax is permitted by specification.
````
## Methods in constructor

View file

@ -4,7 +4,7 @@ Objects allow to store keyed collections of values. That's fine.
But quite often we find that we need an *ordered collection*, where we have a 1st, a 2nd, a 3rd element and so on. For example, we need that to store a list of something: users, goods, HTML elements etc.
It not convenient to use an object here, because it provides no methods to manage the order of elements. We cant insert a new property “between” the existing ones. Objects are just not meant for such use.
It is not convenient to use an object here, because it provides no methods to manage the order of elements. We cant insert a new property “between” the existing ones. Objects are just not meant for such use.
There exists a special data structure named `Array`, to store ordered collections.