function Student(firstName, lastName) {
this.firstName = firstName;
this.lastName = lastName;
};
Object.defineProperty(Student.prototype, "name", {
get: function() {
return this.firstName + ' ' + this.lastName;
}
});
Student.prototype.setSkills = function(skills) {
this.skills = skills || [];
};
class Student {
constructor(firstName, lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
get name() {
return `${this.firstName} ${this.lastName}`;
}
setSkills(skills = []) {
this.skills = skills;
}
}
function foo() {
var apples = 5;
}
{
let apples = 10;
}
{
const apples = 15;
}
// краткий синтаксис
const func = x => x * x;
// блочный синтаксис
const func = (x, y) => { return x + y; };
`строка текста`
`строка текста 1
строка текста 2`
`строка текста ${выражение} строка текста`
var zero, one, two;
var numbers = ['zero', 'one', 'two'];
zero = numbers[0];
one = numbers[1];
two = numbers[2];
const numbers = ['zero', 'one', 'two'];
const [zero, one, two] = numbers;
var p, q;
var obj = { p: 42, q: true };
p = obj.p;
q = obj.q;
const obj = { p: 42, q: true };
const { p, q } = obj;
const promise = new Promise((resolve, reject) => {
setTimeout(() => resolve('result'), 1000);
setTimeout(() => reject(new Error('ignored')), 2000);
});
promise.then(
result => alert('Fulfilled: ' + result),
error => alert('Rejected: ' + error)
);
2 ** 3 // 8
3 ** 2 // 9
3 ** 2.5 // 15.588457268119896
10 ** -1 // 0.1
const str = 'Быть или не быть вот в чём вопрос.';
str.includes('Быть'); // true
str.includes('вопрос'); // true
str.includes('несуществующий'); // false
async function getAuthorAvatar(article) {
const userResponse = await fetch(`/articles/${article}`);
const { author } = await userResponse.json();
const avatarResponse = await fetch(`/avatars/${author.id}`);
const avatar = await avatarResponse.json();
return avatar.url;
}
const numbers = [1, 2, 3, 4, 5];
const [first, second, ...others] = numbers;
console.log(others); // [3, 4, 5]
const others = [3, 4, 5];
const numbers = [1, 2, ...others];
const numbers = {
zero: 1,
one: 2,
three: 3,
four: 4
};
const { zero, one, ...others } = numbers;
console.log(others); // { three: 3, four: 4 }
const others = { three: 3, four: 4 };
const numbers = { zero: 1, one: 2, ...others };
fetch('file.json')
.then(data => data.json())
.catch(error => console.error(error))
.finally(() => console.log('finished'));
// До
try {
...
} catch (error) {
...
}
// После
try {
...
} catch {
...
}
const nestedArrays = ['a', ['b', 'c'], ['d', ['e', 'f']]];
const flattenArray = nestedArrays.flat(2);
// ['a', 'b', 'c', 'd', 'e', 'f']
const words = ['мой любимый', 'ванильный раф'];
const splittedWords = words.map(chunk => chunk.split(' '));
// [['мой', 'любимый'], ['ванильный', 'раф']]
const flattenWords = words.flatMap(chunk => chunk.split(' '));
// ['мой', 'любимый', 'ванильный', 'раф']
const arr = [['name', 'Harry'], ['surname', 'Potter']];
const obj = Object.fromEntries(arr);
// { name: 'Harry', surname: 'Potter' }
const one = ' Привет и позвольте ';
const two = 'нам начать. ';
one.trimStart() + two.trimEnd()
// 'Привет и позвольте нам начать.'
class Cat extends Animal {
#name;
static #count = 0;
static getCount() {
return `На свете ${Cat.#count} кошечек.`;
}
static #increaseCount() {
Cat.#count++;
}
constructor(name = 2) {
super();
this.name = name;
Cat.#increaseCount();
}
}
Можно ли всё это использовать прямо сейчас?


Транспиляция — это процесс перевода исходного кода одного языка в другой


class Student {
constructor(firstName, lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
setSkills(skills = []) {
this.skills = skills;
}
}
var Student = function () {
function Student(firstName, lastName) {
_classCallCheck(this, Student);
this.firstName = firstName;
this.lastName = lastName;
}
_createClass(Student, [{
key: "setSkills",
value: function setSkills() {
var skills =
arguments.length > 0 &&
arguments[0] !== undefined ? arguments[0] : [];
this.skills = skills;
}
}]);
return Student;
}();
const nestedNumbers = {
one: {
two: {
three: 42
}
}
};
const answer = nestedNumbers?.one?.two?.three; // 42
const safe = nestedNumbers?.four?.five?.baz; // undefined

function addNumbers(a, b) {
return a + b;
}
addNumbers(2, 2); // 4
addNumbers(2, '2');
// '22'
addNumbers([], []);
// ''
addNumbers([], {});
// [object Object]
addNumbers({}, []);
// 0
function addNumbers(a, b) {
if (typeof a !== 'number' || typeof b !== 'number') {
throw new Error();
}
return a + b;
}
let isDone: boolean = false;
const decimal: number = 6;
const hex: number = 0xf00d;
const binary: number = 0b1010;
const octal: number = 0o744;
const color1: string = "blue";
const color2: string = 'red';
const fullName: string = `Arkady`;
const sentence: string = `Hello, my name is ${fullName}.`;
const u: undefined = undefined;
const n: null = null;
const list1: number[] = [1, 2, 3];
const list2: Array<number> = [1, 2, 3];
// Объявление кортежа
let point: [number, number];
// Некорректная инициализация
point = [10, 'hello']; // Error
// Инициализация
point = [20, 10]; // OK
// Обращение
point[1]; // 10
// Деструктуризация
const [first, second] = point;
enum Color {
Red,
Green,
Blue
}
const c: Color = Color.Green;
enum Color {
Red = 5,
Green = 7,
Blue = 9
}
enum Color {
Red = '#F00',
Green = '#0F0',
Blue = '#00F'
}
const colors: object = {
red: '#F00',
green: '#0F0',
blue: '#00F'
}
const settings: {
color: string;
delay: number;
retry: boolean;
} = {
color: '#F00',
delay: 2000,
retry: false
};
let notSure: any = 4;
notSure = "maybe a string instead";
notSure = false;
let notSure: any = 4;
notSure.ifItExists(); // Ошибка в рантайме
notSure.toFixed(); // Всё ок, toFixed есть в рантайме
let prettySure: object = 4;
prettySure.toFixed(); // toFixed нет у object
function warnUser(): void {
console.log('This is my warning message');
}
function error(message: string): never {
throw new Error(message);
}
function infiniteLoop(): never {
while (true) {}
}
const someValue: any = 'this is a string';
const strLength1: number = (<string>someValue).length;
const strLength2: number = (someValue as string).length;
function add(x: number, y: number): number {
return x + y;
}
const add = (x: number, y: number): number => x + y;
type ActionGuard = (req: Request) => boolean;
const checkIsAuthorized: ActionGuard = req => {
return Boolean(req.user && req.user.status === 'AUTHORIZED');
};
function printLogin(user: { login: string }) {
console.log(user.login);
}
const user = {
login: 'savichev'
};
printLogin(user);
interface IUser {
login: string;
}
function printLogin(user: IUser) {
console.log(user.login);
}
const user = {
username: 'savichev'
};
printLogin(user);
interface ISquareConfig {
color?: string;
}
function createSquare(config: SquareConfig) {
const newSquare = { color: "white", area: 100 };
if (config.color) {
newSquare.color = config.color;
}
return newSquare;
}
let mySquare = createSquare({ color: "black" });
interface IMakesSound {
makeSound(): void;
}
class Python implements IMakesSound {
private readonly _length: number;
public get length() {
return this._length / 100;
}
constructor(length: number) {
this._length = length;
}
protected makeSound() {
console.log('Ssssss!');
}
}
abstract class Snake {
private readonly _length: number;
public get length() {
return this._length / 100;
}
constructor(length: number) {
this._length = length;
}
protected abstract makeSound(): void;
}
class Python extends Snake {
private static population = 10000;
public static incrementPopulation() {
Python.population++;
}
constructor(length: number) {
super(length);
Python.incrementPopulation();
}
protected makeSound() {
console.log('Ssssss!');
}
}
var Snake = (function() {
function Snake(length) {
this._length = length;
}
Object.defineProperty(
Snake.prototype,
"length",
{
get: function() {
return this._length / 100;
},
enumerable: true,
configurable: true
}
);
return Snake;
})();
var Python = (function(_super) {
__extends(Python, _super);
function Python(length) {
var _this = _super.call(this, length) || this;
Python.incrementPopulation();
return _this;
}
Python.incrementPopulation = function() {
Python.population++;
};
Python.prototype.makeSound = function() {
console.log("Ssssss!");
};
Python.population = 10000;
return Python;
})(Snake);

const n: number = 42;
const s: string = 'Hello, world!';
const a: number[] = [1, 2, 3, 4];
const n = 42;
const s = 'Hello, world!';
const a = [1, 2, 3, 4];
function greetWith(name: string): string {
return `Hello, ${name}!`;
}
function greetWith(name: string) {
return `Hello, ${name}!`;
}
let shapes = [new Circle(), new Square()];
shapes.push(new Triangle());

let shapes = [new Circle(), new Square()];
// Argument of type 'Triangle'
// is not assignable to parameter of type 'Square | Circle'.
shapes.push(new Triangle());

let shapes = [new Circle(), new Square()];
shapes.push(new Triangle());

let shapes = [new Circle(), new Square()];
shapes.push(new Triangle());

let shapes = [new Circle(), new Square()];
shapes.push(new Triangle());

let shapes = [new Circle(), new Square()];
// Argument of type 'Triangle'
// is not assignable to parameter of type 'Square | Circle'.
shapes.push(new Triangle());

let shapes: Shape[] = [new Circle(), new Square()];
shapes.push(new Triangle());

class Human {
name: string;
}
class Robot {
name: string;
}
let human: Human = new Robot();
🤖
interface IStudent {
age: number;
name: string;
}
interface IWorker {
companyName: string;
}
type IWorkingStudent = IStudent & IWorker;
const student: IWorkingStudent = {
age: 21
name: 'Irina'
companyName: 'Yandex'
};
interface IStudent {
id: number;
age: number;
name: string;
}
interface IWorker {
id: string;
companyName: string;
}
type IWorkingStudent = IStudent & IWorker;
const student: IWorkingStudent = {
// Type 'number' is not assignable to type 'number & string'
id: 1,
...
};
interface IStudent {
learn(): void;
}
interface ITeacher {
learn(): void;
teach(): void;
}
function getPerson(): IStudent | ITeacher {
// ...
}
const person = getPerson();
person.learn(); // Ok
person.teach(); // Compilation error
function padLeft(value: string, padding: any) {
if (typeof padding === 'number') {
return Array(padding + 1).join(' ') + value;
}
if (typeof padding === 'string') {
return padding + value;
}
throw new Error(`Expected string or number, got '${padding}'.`);
}
padLeft('Hello world', 4); // Ok, ' Hello world'
padLeft('Hello world', true); // Runtime error
function padLeft(value: string, padding: string | number) {
// ...
}
padLeft('Hello world', true); // Compilation error