var
, ( ):function f(a) {
if (a < 0) {
var i = 3;
}
console.log(i); // 3
}
f(-1)
let
, :function f(a) {
if (a < 0) {
let i = 3;
}
console.log(i); // ReferenceError: i is not defined
}
f(-1)
function setLevel(newLevel = 0) {
...
}
setLevel(); // newLevel = 0
setLevel(5); // newLevel = 5
setLevel(undefined); // newLevel = 0
function foo({ from, to = 10 }) {
...
}
foo({ from: 1, to: 5 });
foo({ to: 5, from: 1 });
foo({ from: 1 });
function foo(positional, { named1, named2 }) {
...
}
foo(123, { named1: 'abc', named2: 'def' })
foo(123, { named2: 'def', named1: 'abc' })
let { first: f, last: l } = { first: 'Jane', last: 'Doe' };
console.log(f); // 'Jane'
console.log(l); // 'Doe'
let { first: f, last: l } = { first: 'Jane' }; //
let { first: f, last?: l } = { first: 'Jane' }; // ok
console.log(l); // undefined
let { first: f, last: l = 'Unknown' } = { first: 'Jane' }; // ok
console.log(l); // 'Unknown'
undefined
:let { a: x = 1 } = { a: undefined }
console.log(x); // 1
let { foo: f }? = anything; // ok
f
undefined
, anything
undefined
, null
foo
.tmp
):{ foo: foo, bar: bar } = { foo: bar, bar: foo};
[ foo, bar ] = [ bar, foo ];
// Supertype
class Person {
constructor(name) {
this.name = name;
}
describe() {
return "Person called " + this.name;
}
}
// Subtype
class Employee extends Person {
constructor(name, title) {
super.constructor(name);
this.title = title;
}
describe() {
return super.describe() + " (" + this.title + ")";
}
}
let jane = new Employee("Jane", "CTO");
jane instanceof Person; // true
jane instanceof Employee; // true
jane.describe(); // 'Person called Jane (CTO)'
// Supertype
function Person(name) {
this.name = name;
}
Person.prototype.describe = function () {
return "Person called " + this.name;
};
// Subtype
function Employee(name, title) {
Person.call(this, name);
this.title = title;
}
Employee.prototype = Object.create(Person.prototype);
Employee.prototype.constructor = Employee;
Employee.prototype.describe = function () {
return Person.prototype.describe.call(this) + " (" + this.title + ")";
};
class Point {
constructor(x, y) {
this.x = x;
this.y = y;
}
static zero() {
return new Point(0, 0);
}
}
module Math {
export function sum(x, y) {
return x + y;
}
export var pi = 3.141593;
//
function internal() {
...
}
}
import Math.{sum, pi};
alert("2π = " + sum(pi, pi));
*
, :import Math.*;
alert("2π = " + sum(pi, pi));
module Widgets {
module Button { ... }
module Alert { ... }
module TextArea { ... }
...
}
import Widgets.Alert.{messageBox, confirmDialog};
...
module JSON = require('http://json.org/modules/json2.js'); // web
import JSON.*;
module File = require('io/File'); // file system
import require("bar.js").y; // file system
for-of
for-in
JavaScript ( ). .. , :let arr = [ "blue", "green" ];
arr.notAnIndex = 123;
Array.prototype.protoProp = 456;
for(var x in arr) {
console.log(x); // blue, green, notAnIndex, protoProp
}
for-of
, :for(var x of arr) {
console.log(x); // blue, green
}
yield
, .let squares = [ 1, 2, 3 ].map(x => x * x);
let squares = [ 1, 2, 3 ].map(function (x) { return x * x });
this
. ..let jane = {
name: "Jane",
sayHello: function (friends) {
friends.forEach(friend => { console.log(this.name + " says hello to " + friend) });
}
}
jane.sayHello([ 'Mark', 'John' ]);
Jane says hello to Mark
Jane says hello to John
let jane = {
name: "Jane",
sayHello: function (friends) {
friends.forEach(function(friend) { console.log(this.name + " says hello to " + friend) });
}
}
says hello to Mark
says hello to John
this
function(friend) { ... })
this
. , , var self = this
bind
:var jane = {
name: "Jane",
sayHello: function (friends) {
friends.forEach(function (friend) {
console.log(this.name + " says hello to " + friend)
}.bind(this));
}
}
(x, y) => x + y + this.z
function (x, y) { return x + y + this.z }.bind(this)
new (() => {})
)arguments
( )typeof
instanceof
:typeof () => {}; // 'function'
() => {} instanceof Function; // true
Source: https://habr.com/ru/post/175371/
All Articles