up
|
@ -212,7 +212,7 @@ If you are writing several "helper" functions and the code to use them, then the
|
||||||
}
|
}
|
||||||
|
|
||||||
// *!*the code which uses them*/!*
|
// *!*the code which uses them*/!*
|
||||||
var elem = createElement();
|
let elem = createElement();
|
||||||
setHandler(elem);
|
setHandler(elem);
|
||||||
walkAround();
|
walkAround();
|
||||||
```
|
```
|
||||||
|
@ -220,7 +220,7 @@ If you are writing several "helper" functions and the code to use them, then the
|
||||||
|
|
||||||
```js
|
```js
|
||||||
// *!*the code which uses the functions*/!*
|
// *!*the code which uses the functions*/!*
|
||||||
var elem = createElement();
|
let elem = createElement();
|
||||||
setHandler(elem);
|
setHandler(elem);
|
||||||
walkAround();
|
walkAround();
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
describe("multiplyNumeric", function() {
|
describe("multiplyNumeric", function() {
|
||||||
it("multiplies all numeric properties by 2", function() {
|
it("multiplies all numeric properties by 2", function() {
|
||||||
var menu = {
|
let menu = {
|
||||||
width: 200,
|
width: 200,
|
||||||
height: 300,
|
height: 300,
|
||||||
title: "My menu"
|
title: "My menu"
|
||||||
|
|
|
@ -10,8 +10,8 @@ Is it possible to create functions `A` and `B` such as `new A()==new B()`?
|
||||||
function A() { ... }
|
function A() { ... }
|
||||||
function B() { ... }
|
function B() { ... }
|
||||||
|
|
||||||
var a = new A;
|
let a = new A;
|
||||||
var b = new B;
|
let b = new B;
|
||||||
|
|
||||||
alert( a == b ); // true
|
alert( a == b ); // true
|
||||||
```
|
```
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
# Using "new" to create objects
|
# Constructor, operator "new"
|
||||||
|
|
||||||
The regular `{...}` syntax allows to create one object. But often we need to create many similar objects.
|
The regular `{...}` syntax allows to create one object. But often we need to create many similar objects.
|
||||||
|
|
||||||
|
@ -211,6 +211,3 @@ john = {
|
||||||
We can use constructor functions to make multiple similar objects. But the topic is much deeper than described here. So we'll return it later and cover more in-depth.
|
We can use constructor functions to make multiple similar objects. But the topic is much deeper than described here. So we'll return it later and cover more in-depth.
|
||||||
|
|
||||||
As of now, it's important to understand what `new` is, because JavaScript provides constructor functions for many built-in language objects: like `Date` for dates, `Set` for sets and others that we plan to study.
|
As of now, it's important to understand what `new` is, because JavaScript provides constructor functions for many built-in language objects: like `Date` for dates, `Set` for sets and others that we plan to study.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -22,7 +22,7 @@ How to delete an element from the array?
|
||||||
The arrays are objects, so we can try to use `delete`:
|
The arrays are objects, so we can try to use `delete`:
|
||||||
|
|
||||||
```js run
|
```js run
|
||||||
var arr = ["I", "go", "home"];
|
let arr = ["I", "go", "home"];
|
||||||
|
|
||||||
delete arr[1]; // remove "go"
|
delete arr[1]; // remove "go"
|
||||||
|
|
||||||
|
|
|
@ -36,7 +36,7 @@ Letter-sorting is done by the chain of calls in the line `(*)`.
|
||||||
For convenience let's split it into multiple lines:
|
For convenience let's split it into multiple lines:
|
||||||
|
|
||||||
```js
|
```js
|
||||||
var sorted = arr[i] // PAN
|
let sorted = arr[i] // PAN
|
||||||
.toLowerCase() // pan
|
.toLowerCase() // pan
|
||||||
.split('') // ['p','a','n']
|
.split('') // ['p','a','n']
|
||||||
.sort() // ['a','n','p']
|
.sort() // ['a','n','p']
|
||||||
|
@ -61,10 +61,10 @@ That's how the solution can look:
|
||||||
|
|
||||||
```js run
|
```js run
|
||||||
function aclean(arr) {
|
function aclean(arr) {
|
||||||
var obj = {};
|
let obj = {};
|
||||||
|
|
||||||
for (var i = 0; i < arr.length; i++) {
|
for (let i = 0; i < arr.length; i++) {
|
||||||
var sorted = arr[i].toLowerCase().split("").sort().join("");
|
let sorted = arr[i].toLowerCase().split("").sort().join("");
|
||||||
obj[sorted] = arr[i];
|
obj[sorted] = arr[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -75,7 +75,3 @@ let arr = ["nap", "teachers", "cheaters", "PAN", "ear", "era", "hectares"];
|
||||||
|
|
||||||
alert( aclean(arr) );
|
alert( aclean(arr) );
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
function getWeekDay(date) {
|
function getWeekDay(date) {
|
||||||
var days = ['SU', 'MO', 'TU', 'WE', 'TH', 'FR', 'SA'];
|
let days = ['SU', 'MO', 'TU', 'WE', 'TH', 'FR', 'SA'];
|
||||||
|
|
||||||
return days[date.getDay()];
|
return days[date.getDay()];
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
function getLocalDay(date) {
|
function getLocalDay(date) {
|
||||||
|
|
||||||
var day = date.getDay();
|
let day = date.getDay();
|
||||||
|
|
||||||
if (day == 0) { // weekday 0 (sunday) is 7 in european
|
if (day == 0) { // weekday 0 (sunday) is 7 in european
|
||||||
day = 7;
|
day = 7;
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
```js run
|
```js run
|
||||||
function getLocalDay(date) {
|
function getLocalDay(date) {
|
||||||
|
|
||||||
var day = date.getDay();
|
let day = date.getDay();
|
||||||
|
|
||||||
if (day == 0) { // 0 becomes 7
|
if (day == 0) { // 0 becomes 7
|
||||||
day = 7;
|
day = 7;
|
||||||
|
|
|
@ -18,8 +18,8 @@ describe("getDateAgo", function() {
|
||||||
});
|
});
|
||||||
|
|
||||||
it("does not modify the given date", function() {
|
it("does not modify the given date", function() {
|
||||||
var date = new Date(2015, 0, 2);
|
let date = new Date(2015, 0, 2);
|
||||||
var dateCopy = new Date(date);
|
let dateCopy = new Date(date);
|
||||||
getDateAgo(dateCopy, 100);
|
getDateAgo(dateCopy, 100);
|
||||||
assert.equal(date.getTime(), dateCopy.getTime());
|
assert.equal(date.getTime(), dateCopy.getTime());
|
||||||
});
|
});
|
||||||
|
|
|
@ -12,7 +12,7 @@ function formatDate(date) {
|
||||||
return sec + ' sec. ago';
|
return sec + ' sec. ago';
|
||||||
}
|
}
|
||||||
|
|
||||||
var min = Math.floor(diff / 60000); // convert diff to minutes
|
let min = Math.floor(diff / 60000); // convert diff to minutes
|
||||||
if (min < 60) {
|
if (min < 60) {
|
||||||
return min + ' min. ago';
|
return min + ' min. ago';
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,7 +14,7 @@ function formatDate(date) {
|
||||||
return sec + ' sec. ago';
|
return sec + ' sec. ago';
|
||||||
}
|
}
|
||||||
|
|
||||||
var min = Math.floor(diff / 60000); // convert diff to minutes
|
let min = Math.floor(diff / 60000); // convert diff to minutes
|
||||||
if (min < 60) {
|
if (min < 60) {
|
||||||
return min + ' min. ago';
|
return min + ' min. ago';
|
||||||
}
|
}
|
||||||
|
|
Before Width: | Height: | Size: 16 KiB After Width: | Height: | Size: 16 KiB |
Before Width: | Height: | Size: 40 KiB After Width: | Height: | Size: 40 KiB |
Before Width: | Height: | Size: 6.8 KiB After Width: | Height: | Size: 6.8 KiB |
Before Width: | Height: | Size: 16 KiB After Width: | Height: | Size: 16 KiB |
Before Width: | Height: | Size: 10 KiB After Width: | Height: | Size: 10 KiB |
Before Width: | Height: | Size: 26 KiB After Width: | Height: | Size: 26 KiB |
Before Width: | Height: | Size: 9.6 KiB After Width: | Height: | Size: 9.6 KiB |
Before Width: | Height: | Size: 23 KiB After Width: | Height: | Size: 23 KiB |
Before Width: | Height: | Size: 5.3 KiB After Width: | Height: | Size: 5.3 KiB |
Before Width: | Height: | Size: 13 KiB After Width: | Height: | Size: 13 KiB |
Before Width: | Height: | Size: 19 KiB After Width: | Height: | Size: 19 KiB |
Before Width: | Height: | Size: 46 KiB After Width: | Height: | Size: 46 KiB |
Before Width: | Height: | Size: 30 KiB After Width: | Height: | Size: 30 KiB |
Before Width: | Height: | Size: 70 KiB After Width: | Height: | Size: 70 KiB |
Before Width: | Height: | Size: 26 KiB After Width: | Height: | Size: 26 KiB |
Before Width: | Height: | Size: 59 KiB After Width: | Height: | Size: 59 KiB |
|
@ -524,7 +524,7 @@ Lexical Environment objects that we've been talking about are subjects to same m
|
||||||
|
|
||||||
```js
|
```js
|
||||||
function f() {
|
function f() {
|
||||||
var value = Math.random();
|
let value = Math.random();
|
||||||
|
|
||||||
return function() {};
|
return function() {};
|
||||||
}
|
}
|
||||||
|
@ -538,7 +538,7 @@ Lexical Environment objects that we've been talking about are subjects to same m
|
||||||
|
|
||||||
```js
|
```js
|
||||||
function f() {
|
function f() {
|
||||||
var value = 123;
|
let value = 123;
|
||||||
|
|
||||||
function g() {}
|
function g() {}
|
||||||
|
|
||||||
|
@ -565,7 +565,7 @@ When it pauses, in console type `alert(value)`.
|
||||||
|
|
||||||
```js run
|
```js run
|
||||||
function f() {
|
function f() {
|
||||||
var value = Math.random();
|
let value = Math.random();
|
||||||
|
|
||||||
function g() {
|
function g() {
|
||||||
debugger; // in console: type alert( value ); No such variable!
|
debugger; // in console: type alert( value ); No such variable!
|
||||||
|
@ -574,7 +574,7 @@ function f() {
|
||||||
return g;
|
return g;
|
||||||
}
|
}
|
||||||
|
|
||||||
var g = f();
|
let g = f();
|
||||||
g();
|
g();
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -623,7 +623,7 @@ From the first sight, `var` behaves similar to `let`. That is, declares a variab
|
||||||
|
|
||||||
```js run
|
```js run
|
||||||
function sayHi() {
|
function sayHi() {
|
||||||
var phrase = "Hello"; // local variable, "var" instead of "let"
|
let phrase = "Hello"; // local variable, "var" instead of "let"
|
||||||
|
|
||||||
alert(phrase); // Hello
|
alert(phrase); // Hello
|
||||||
}
|
}
|
||||||
|
@ -640,7 +640,7 @@ alert(phrase); // Error, phrase is not defined
|
||||||
|
|
||||||
```js
|
```js
|
||||||
if (true) {
|
if (true) {
|
||||||
var test = true; // use "var" instead of "let"
|
let test = true; // use "var" instead of "let"
|
||||||
}
|
}
|
||||||
|
|
||||||
*!*
|
*!*
|
||||||
|
@ -653,7 +653,7 @@ alert(phrase); // Error, phrase is not defined
|
||||||
The same thing for loops: `var` can not be block or loop-local:
|
The same thing for loops: `var` can not be block or loop-local:
|
||||||
|
|
||||||
```js
|
```js
|
||||||
for(var i = 0; i < 10; i++) {
|
for(let i = 0; i < 10; i++) {
|
||||||
// ...
|
// ...
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -667,7 +667,7 @@ alert(phrase); // Error, phrase is not defined
|
||||||
```js
|
```js
|
||||||
function sayHi() {
|
function sayHi() {
|
||||||
if (true) {
|
if (true) {
|
||||||
var phrase = "Hello";
|
let phrase = "Hello";
|
||||||
}
|
}
|
||||||
|
|
||||||
alert(phrase); // works
|
alert(phrase); // works
|
||||||
|
@ -691,7 +691,7 @@ alert(phrase); // Error, phrase is not defined
|
||||||
alert(phrase);
|
alert(phrase);
|
||||||
|
|
||||||
*!*
|
*!*
|
||||||
var phrase;
|
let phrase;
|
||||||
*/!*
|
*/!*
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
@ -701,7 +701,7 @@ alert(phrase); // Error, phrase is not defined
|
||||||
```js
|
```js
|
||||||
function sayHi() {
|
function sayHi() {
|
||||||
*!*
|
*!*
|
||||||
var phrase;
|
let phrase;
|
||||||
*/!*
|
*/!*
|
||||||
phrase = "Hello";
|
phrase = "Hello";
|
||||||
|
|
||||||
|
@ -717,7 +717,7 @@ alert(phrase); // Error, phrase is not defined
|
||||||
|
|
||||||
*!*
|
*!*
|
||||||
if (false) {
|
if (false) {
|
||||||
var phrase;
|
let phrase;
|
||||||
}
|
}
|
||||||
*/!*
|
*/!*
|
||||||
|
|
||||||
|
@ -738,21 +738,21 @@ alert(phrase); // Error, phrase is not defined
|
||||||
alert(phrase);
|
alert(phrase);
|
||||||
|
|
||||||
*!*
|
*!*
|
||||||
var phrase = "Hello";
|
let phrase = "Hello";
|
||||||
*/!*
|
*/!*
|
||||||
}
|
}
|
||||||
|
|
||||||
sayHi();
|
sayHi();
|
||||||
```
|
```
|
||||||
|
|
||||||
The line `var phrase = "Hello"` has two actions in it: variable declaration `var` and assignment `=`.
|
The line `let phrase = "Hello"` has two actions in it: variable declaration `var` and assignment `=`.
|
||||||
|
|
||||||
The declaration is processed at the start of function execution (hoisted), but the assignment is not. So the code works essentially like this:
|
The declaration is processed at the start of function execution (hoisted), but the assignment is not. So the code works essentially like this:
|
||||||
|
|
||||||
```js run
|
```js run
|
||||||
function sayHi() {
|
function sayHi() {
|
||||||
*!*
|
*!*
|
||||||
var phrase; // variable is declared from the top...
|
let phrase; // variable is declared from the top...
|
||||||
*/!*
|
*/!*
|
||||||
|
|
||||||
alert(phrase); // undefined
|
alert(phrase); // undefined
|
Before Width: | Height: | Size: 22 KiB After Width: | Height: | Size: 22 KiB |