Основы

Types#

Просмотреть видео ниже. После каждой под-темы ставить видео на паузу и повторять набранный лектором код.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>JavaScript Intro</title>
</head>
<body>
<script>
let alex;
class Contact {
constructor(fistNameOrObject, lastName, email) {
if (typeof fistNameOrObject === 'object') {
Object.assign(this, fistNameOrObject);
} else {
this.fistName = fistNameOrObject;
this.lastName = lastName;
this.email = email;
}
}
toString() {
return `${this.fistName} ${this.lastName} <${this.email}>`;
}
}
alex = new Contact('Alex', 'Kotov', 'alexko@in6k.com');
console.log('in main', '' + alex);
let vladik = new Contact({
fistName: 'Vladik',
lastName: 'Nikolenko',
email: 'vladik@in6k.com',
});
console.log('in main', '' + vladik);
let colors = { red: '#ff0000', blank: '#000000', white: '#ffffff' };
for (let name in colors) {
console.log(name + ' has code ' + colors[name].toUpperCase());
}
</script>
</body>
</html>

var vs let vs const#

Debugging#

https://code.visualstudio.com/docs/nodejs/nodejs-debugging

Branches and loops#

0 of 3

Typical issues#

  • no return fn sum(a, b) { return a + b; } command query separation principle
  • global variables var result = ''; fn(s) { result += s;}
  • extra code return age >= 18 ? true : false;
  • big complex if
if(count === 0 || count <=20 && count >= 5 || count % 10 === 0 || count % 10 <=19 && count % 10 >= 5 || count % 100 == 20){
return count + ' ' + many;
}
  • code formatting
function plural(count, one, few, many) {
if(count%10 == 1 && count%100 != 11){
return (count+" "+one);
}else if (count%100 == 11 || count%10 === 0 || 5<=count%10 && count%10<=9){
return(count+" "+many);
}else{
return(count+" "+few);
}
}
  • code duplication
if (11 <= count && count <= 14) return count +' ' + many;
----11---------count---------19------------->
if (count === 11)
if (count === 12) return count +' ' + many;
if (count === 13) return count +' ' + many;
if (count === 14) return count +' ' + many;
  • naming dob, suma: better to use result
  • "рычащие" название переменных
let newarr = [...arr]
let words = newarr.slice(1, 4)