Created by
let
and const
let
and const
Read more for let/const in the Beginners Course Slides
const pi = 3.14;
// function declaration with expression:
let circleAreaExp = function(r){
return r*r*pi;
}
// function declaration with arrow syntax:
let circleAreaArr = r=>r*r*pi;
console.log( circleAreaArr(10) );
console.log( circleAreaExp(10) );
(param1, param2, …, paramN) => { statements }
let circleArea = (r)=>{return r*r*pi};
console.log(`circleArea(2) = ${circleArea(2)}`);
singleParam => { statements }
let circleArea = r=>{return r*r*pi};
console.log(`circleArea(2) = ${circleArea(2)}`);
(param1, param2, …, paramN) => expression
let circleArea = r=>r*r*pi;
console.log(`circleArea(2) = ${circleArea(2)}`);
let calculator1 = {
'add': (a,b) => a+b,
'sub': (a,b) => a-b,
'mult': (a,b) => a*b,
'div': (a,b) => a/b,
}
let calculator2 = {
'add': function(a,b){return a+b},
'sub': function(a,b){return a-b},
'mult': function(a,b){return a*b},
'div': function(a,b){return a/b},
}
console.log( calculator1.add(2,3) );
console.log( calculator2.add(2,3) );
arguments
object in arrow function is reference to the arguments object in the enclosing scope
const dirFuncExprArgs = function() {
console.dir(arguments);
}
let dirArrowArgs = ()=>{
console.dir(arguments);
}
dirFuncExprArgs(1,2,3); // [1,2,3]
dirArrowArgs(1,2,3); // error in browsers
function foo() {
console.log(`arguments in foo:`);
console.dir(arguments);
const bar = () => {
console.log(`arguments in bar:`);
console.dir(arguments);
}
bar(4,5,6);
}
foo(1,2,3);
// arguments in foo:
// [Arguments] { '0': 1, '1': 2, '2': 3 }
// arguments in bar:
// [Arguments] { '0': 1, '1': 2, '2': 3 }
call/apply or bind
methods!the "this" value, as well as the call/apply/bind
methods are discussed in further topics.
lexicalThis = this;
var obj = {
'exp': function(){
console.log(this === lexicalThis);
},
'arr': ()=>{console.log(this === lexicalThis)}
}
obj.exp(); // false
obj.arr(); // true
let user = {
'userName': 'Ada Byron',
'greet': function () {
console.log(`Hello, I'm ${this.userName}`);
},
'greetArr': () => console.log(`Hello, I'm ${this.userName}`)
}
user.greet();
user.greetArr();
// Hello, I'm Ada Byron
// Hello, I'm undefined
let cb;
cb = cb || function() {}; // ok
cb = cb || () => {};
// SyntaxError: invalid arrow-function arguments
cb = cb || (() => {}); // ok
this.handleClick = this.handleClick.bind(this)
in the constructor.
function f(x, y, z){
x = x || 1;
y = y || 2;
z = z || 3;
console.log(x, y, z); //6,7,3
}
f(6, 7);
function f(x=1, y=2, z=3){
console.log(x, y, z); //6,7,3
}
f(6, 7);
myFunction(...iterableObj);
[...iterableObj, 4, 5, 6];
@@iterator
method (more on MDN...)
function foo(a,b,c){
console.log(`a=${a}`)
console.log(`b=${b}`)
console.log(`c=${c}`)
}
var args = [1,2,3];
foo(...args);
//a=1
//b=2
//c=3
let arr1 = [1,2,3];
let arr2 = [4,5];
let arrNew = [ ...arr1, ...arr2];
console.log( arrNew );
// [ 1, 2, 3, 4, 5 ]
arguments
objectarguments
object, the ...rest
parameter is an array type
function f(a, b, ...args){
console.log(args); //"3, 4, 5"
}
f(1, 2, 3, 4, 5);
// assign variables from array:
let [a, b] = [1,2];
console.log(`a = ${a}; b = ${b}`); // a = 1; b = 2
// same as above - no matter that we pass more elements:
let [a, b] = [1,2,3,4,5];
console.log(`a = ${a}; b = ${b}`); // a = 1; b = 2
// assign variables from array in conjunction with rest syntax:
let [a, ...rest] = [1,2,3];
console.log(a); // 1
console.log(rest); // [2, 3]
let a = 1, b = 2;
// we do the swap without tmp variable, just with one line:
[a,b] = [b,a];
console.log(`a = ${a}; b = ${b}`); // a = 2; b = 1
let obj = {p1: 1, p2: 2};
// assign obj properties to variables with same name:
let {p1, p2} = obj;
console.log(p1); // 1
console.log(p2); // 2
let userName = 'pesho';
let userAge = 23;
// ES6 way:
let p1 = {userName, userAge};
// ES5 way:
// let p1 = {userName:userName, userAge:userAge};
console.log(p1); // { userName: 'pesho', userAge: 23 }
let p1 = {
name: 'Pesho',
greet(){
console.log(`Hi, I'm ${this.name}`);
}
}
p1.greet(); // Hi, I'm Pesho
These slides are based on
customised version of
framework