function min(a, b) {
return a < b ? a : b;
}
min(2, 7); // 2
min(3, 4, 2); // 3
min(13); // undefined
function min(a, b) {
return a > b ? b : a;
}
min(2, 7); // 2
min(13); // 13
function min(a, b) {
if (b === undefined) {
return a;
}
return a < b ? a : b;
}
min(2, 7); // 2
min(13); // 13
function min(a, b = Infinity) {
return a < b ? a : b;
}
min(2, 7); // 2
min(13); // 13
function log(a = 'hello') {
console.log(a);
}
log(); // hello
log(0); // 0
log(undefined); // hello
log(null); // null
function BMI(params) {
const { weight, height } = params;
return weight / (height * height);
}
BMI({ weight: 60, height: 1.7 }) // 20.7
function BMI(params) {
const { weight, height } = params;
return weight / (height * height);
}
BMI({ weight: 60, height: 1.7 }) // 20.7
Объект arguments - это подобный массиву объект, который содержит аргументы, переданные в функцию.
function example() {
arguments[1]; // 12
arguments.length; // 2
}
example(3, 12);
function sum() {
let sum = 0;
for (let i = 0; i < arguments.length; i++) {
sum += arguments[i];
}
return sum;
}
sum(); // 0
sum(2); // 2
sum(2, 4, 8); // 14
В браузере:
const func = () => console.log(arguments);
func(); // ReferenceError: arguments is not defined
function argumentsTest(str) {
console.log(arguments);
const arrowFunc = (a, b) => {
console.log(arguments);
}
arrowFunc(1, 2);
}
function argumentsTest(str) {
console.log(arguments); // [Arguments] { '0': 'test' }
const arrowFunc = (a, b) => {
console.log(arguments); // [Arguments] { '0': 'test' }
}
arrowFunc(1, 2);
}
argumentsTest('test');
function example(a, b, ...others) {
console.log(a);
console.log(b);
console.log(others);
}
example(1, 2, 3, 4, 5);
// 1
// 2
// [3, 4, 5]
function sum(...numbers) {
return numbers.reduce((sum, item) => {
return sum + item;
}, 0);
}
sum(1, 2, 3); // 6
// function declaration
function add(a, b) {
return a + b;
}
// function expression
const add = function (a, b) {
return a + b;
}
add(2, 3);
function add(a, b) {
return a + b;
}
add(2, 3); // 5
function add(a, b) {
return a + b;
}
add(2, 3);
const add = function (a, b) {
return a + b;
};
add(2, 3); // ReferenceError: add is not defined
const add = function (a, b) {
return a + b;
};
const factorial = function inner(n) {
return n === 1 ?
1 : n * inner(n - 1);
}
typeof factorial; // 'function'
typeof inner; // undefined
factorial(3); // 6
Код:
const text = 'Привет';
function greet() {}
Область видимости:
┌{ text, greet }
│
│
│
Код:
function greet() {
const text = 'Привет';
text; // 'Привет'
}
Область видимости:
┌{ greet }
├─┬{ text }
│ │
│ │
│
│
│
Код:
function greet() {
const text = 'Привет';
text; // 'Привет'
}
text; // ReferenceError:
// text is not defined
Область видимости:
┌{ greet }
├─┬{ text }
│ │
│ │
│
│
│
Код:
function greet() {
let text = 'Привет';
function nested() {
text; // 'Привет'
}
}
Область видимости:
┌{ greet }
├─┬{ text, nested }
│ │
│ │
│ ├─┬{ }
│ │ │
│ │
│
Код:
function greet() {
let text = 'Привет';
function nested() {
let text = 'Пока';
text; // 'Пока'
}
text; // 'Привет'
}
Область видимости:
┌{ greet }
├─┬{ text: Привет, nested }
│ │
│ │
│ ├─┬{ text: Пока }
│ │ │
│ │ │
│ │
│ │
│ │
│
let x = 10;
var y = 10;
{
let x = 5;
var y = 5;
{
let x = 2;
var y = 2;
console.log(x);
console.log(y);
}
console.log(x);
console.log(y);
}
console.log(x);
console.log(y);
let x = 10;
var y = 10;
{
let x = 5;
var y = 5;
{
let x = 2;
var y = 2;
console.log(x); // 2
console.log(y); // 2
}
console.log(x);
console.log(y);
}
console.log(x);
console.log(y);
let x = 10;
var y = 10;
{
let x = 5;
var y = 5;
{
let x = 2;
var y = 2;
console.log(x); // 2
console.log(y); // 2
}
console.log(x); // 5
console.log(y); // 2
}
console.log(x);
console.log(y);
let x = 10;
var y = 10;
{
let x = 5;
var y = 5;
{
let x = 2;
var y = 2;
console.log(x); // 2
console.log(y); // 2
}
console.log(x); // 5
console.log(y); // 2
}
console.log(x); // 10
console.log(y); // 2
function declaration
var
add(2, 3); // 5
function add(a, b) {
return a + b;
}
Код:
add(2, 3); // TypeError:
// add is not a function
var add = function (a, b) {
return a + b;
}
Интерпретатор:
var add;
add(2, 3);
add = function (a, b) {
return a + b;
};
add(2, 3); // ReferenceError: add is not defined
const add = function (a, b) {
return a + b;
}
Замыкание – это функция вместе со всеми внешними переменными, которые ей доступны.
Код:
function greet() {
const text = 'Привет';
}
greet();
Счётчик ссылок:
{ text: 1 }
Код:
function greet() {
const text = 'Привет';
}
greet();
Счётчик ссылок:
{ text: 0 }
Код:
function makeCounter() {
let currentCount = 0;
return function () {
return currentCount++;
};
}
const counter = makeCounter();
Счётчик ссылок:
{ currentCount: 1 }
Код:
function makeCounter() {
let currentCount = 0;
return function () {
return currentCount++;
};
}
const counter = makeCounter();
Счётчик ссылок:
{ currentCount: 1 }
Код:
function makeCounter() {
let currentCount = 0;
return function () {
return currentCount++;
};
}
Область видимости:
┌{ makeCounter }
├─┬{ currentCount }
│ │
│ │
│ ├─┬{ }
│ │ │
│ │
│
const counter = makeCounter();
counter(); // 0
counter(); // 1
counter(); // 2
const yetAnother = makeCounter();
yetAnother(); // 0
function greet(name) {
return function () {
return `Привет, ${name}`;
}
}
let helloWorld = greet('мир!');
helloWorld(); // "Привет, мир!"
public class User {
private int age = 30;
public void showAge() {
System.out.println(this.age);
}
}
public static void main(String []args){
User vasya = new User();
vasya.showAge();
}
class User () {
constructor() {
this.age = 24;
}
showAge() {
return this.age;
}
}
const mike = new User();
mike.showAge(); // 24
В браузере
this.innerWidth; // 1440
В Node.js
this.process.version; // 'v10.8.0'
Код:
function sum(a, b) {
return a + b;
}
sum(1, 2);
Область видимости:
┌{ sum } // 1
├─┬{ a, b } // 2
│ │
│ │
│
│
Код:
function sum(a, b) {
return a + b;
}
sum(1, 2);
Контекст исполнения:
┌{ lE: { sum }, this: ??? }
├─┬{ lE: { a, b }, this: ??? }
│ │
│ │
│
│
Значение this зависит от:
this.innerWidth; // 1440
window.innerWidth; // 1440
innerWidth; // 1440
this.process.version; // 'v10.8.0'
global.process.version; // 'v10.8.0'
process.version; // 'v10.8.0'
console.log('Hello!');
global.console.log('Hello!');
this.console.log('Hello!');
this === global; // true
function getSelf() {
return this;
}
getSelf(); // global
Код:
const block = {
innerHeight: 200,
getHeight: function () {
return this.innerHeight;
}
}
Значение this:
???.innerHeight;
Код:
const block = {
innerHeight: 200,
getHeight: function () {
return this.innerHeight;
}
}
block.getHeight(); // 200
Значение this:
block.innerHeight;
Код:
const block = {
innerHeight: 200,
getHeight: function () {
return this.innerHeight;
}
}
const getHeight = block.getHeight;
Значение this:
???.innerHeight;
Код:
const block = {
innerHeight: 200,
getHeight: function () {
return this.innerHeight;
}
}
const getHeight = block.getHeight;
getHeight();
Значение this:
window.innerHeight;
Метод call() вызывает функцию с указанным значением this и индивидуально предоставленными аргументами.
Function.prototype.call() - JavaScript | MDN
fun.call(thisArg, arg1, arg2, ...);
Код:
const mike = {
age: 30,
getAge: function () {
return this.age;
}
};
const jane = {
age: 24
};
Значение this:
???.age;
Код:
const mike = {
age: 30,
getAge: function () {
return this.age;
}
};
const jane = {
age: 24
};
mike.getAge.call(jane); // 24
Значение this:
jane.age;
Метод apply() вызывает функцию с указанным значением this и аргументами, предоставленными в виде массива.
Function.prototype.apply() - JavaScript | MDN
fun.apply(thisArg, [arg1, arg2]);
Math.min(4, 7, 2, 9); // 2
const numbers = [4, 7, 2, 9];
Math.min(arr); // NaN
Math.min.apply(Math, numbers); // 2
Math.min.apply(null, numbers); // 2
const numbers = [4, 7, 2, 9];
Math.min(...numbers); // 2
const person = {
name: 'Jack',
items: ['keys', 'phone', 'banana'],
showItems: function () {
return this.items.map(function (item) {
return `${this.name} has ${item}`;
});
}
}
???.items
???.name
const person = {
name: 'Jack',
items: ['keys', 'phone', 'banana'],
showItems: function () {
return this.items.map(function (item) {
return `${this.name} has ${item}`;
});
}
}
person.showItems();
person.items
???.name
const person = {
name: 'Jack',
items: ['keys', 'phone', 'banana'],
showItems: function () {
return this.items.map(function (item) {
return `${this.name} has ${item}`;
});
}
}
person.showItems();
person.items
global.name
'undefined has keys'
'undefined has phone'
'undefined has banana'
const person = {
name: 'Jack',
items: ['keys', 'phone', 'banana'],
showItems: function () {
return this.items.map(item => {
return `${this.name} has ${item}`;
});
}
}
person.showItems();
person.items
person.name
'Jack has keys'
'Jack has phone'
'Jack has banana'
const person = {
name: 'Jack',
items: ['keys', 'phone', 'banana'],
showItems: function () {
return this.items.map(function (item) {
return `${this.name} has ${item}`;
}, this);
}
}
person.showItems();
person.items
person.name
Метод bind()
создаёт новую функцию,
которая при вызове устанавливает в качестве контекста выполнения
this предоставленное значение. <...>
Function.prototype.bind() - JavaScript | MDN
fun.bind(thisArg, arg1, arg2, ...);
const person = {
name: 'Jack',
items: ['keys', 'phone', 'banana'],
showItems: function () {
return this.items.map(function (item) {
return this.name + ' has ' + item;
}.bind(this));
}
}
???.items
???.name
const person = {
name: 'Jack',
items: ['keys', 'phone', 'banana'],
showItems: function () {
return this.items.map(function (item) {
return this.name + ' has ' + item;
}.bind(this));
}
}
person.showItems();
person.items
person.name
Math.pow(2, 3); // 8
Math.pow(2, 10); // 1024
const binPow = Math.pow.bind(null, 2);
binPow(3); // 8
binPow(10); // 1024
function getSelf() {
return this;
}
getSelf(); // global
function getSelf() {
'use strict';
return this;
}
getSelf(); // undefined