Showing posts with label ES6. Show all posts
Showing posts with label ES6. Show all posts

Wednesday, July 1, 2020

ES6 Computed Property Names

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Object_initializer#Computed_property_names

// Computed property names (ES2015)
let i = 0
let a = {
  ['foo' + ++i]: i,
  ['foo' + ++i]: i,
  ['foo' + ++i]: i
}

console.log(a.foo1) // 1
console.log(a.foo2) // 2
console.log(a.foo3) // 3

Wednesday, February 13, 2019

ES6 - Chaining functions using Generator Function

Chaining functions using Generator Function

function* doSomething2() {
  yield* [
    () => {
      console.log("hello2");
      gen2.next();
      gen2.next().value();
     },
    () => {
      console.log("world2");
    },
    () => {
      console.log("world22");
    },
  ];
}

var gen2 = doSomething2();

gen2.next().value();


Output:
---------
hello2
world22

ES6 related

Friday, March 17, 2017

ES6 - const (for objects and arrays)

Excerpt from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/const


// const also works on objects
const MY_OBJECT = {'key': 'value'};

// Attempting to overwrite the object throws an error
MY_OBJECT = {'OTHER_KEY': 'value'};

// However, object keys are not protected,
// so the following statement is executed without problem
MY_OBJECT.key = 'otherValue'; // Use Object.freeze() to make object immutable

// The same applies to arrays
const MY_ARRAY = [];
// It's possible to push items into the array
MY_ARRAY.push('A'); // ["A"]
// However, assigning a new array to the variable throws an error
MY_ARRAY = ['B']

Monday, March 13, 2017

ES6 - new syntax about 'const'

ES6 - What do function parameter lists inside of curly braces do in es6?


const func = ({ param1, param2 }) => {
    //do stuff
}

It is destructuring, but contained within the parameters. The equivalent without the destructuring would be:

const func = o => {
    var param1 = o.param1;
    var param2 = o.param2;
    //do stuff
}

It is basically shorthand for {param1: param1, param2: param2}


Friday, February 17, 2017

ES6 - Arrow Functions


https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Functions/Arrow_functions
https://hacks.mozilla.org/2015/06/es6-in-depth-arrow-functions/

Syntax

Basic Syntax

(param1, param2, …, paramN) => { statements }
(param1, param2, …, paramN) => expression
// equivalent to: (param1, param2, …, paramN) => { return expression; }

// Parentheses are optional when there's only one parameter:
(singleParam) => { statements }
singleParam => { statements }

// A function with no parameters requires parentheses:
() => { statements }
() => expression // equivalent to: () => { return expression; }

Advanced Syntax

// Parenthesize the body to return an object literal expression:
params => ({foo: bar})

// Rest parameters and default parameters are supported
(param1, param2, ...rest) => { statements }
(param1 = defaultValue1, param2, …, paramN = defaultValueN) => { statements }

// Destructuring within the parameter list is also supported
var f = ([a, b] = [1, 2], {x: c} = {x: a + b}) => a + b + c;
f();  // 6
Detailed syntax examples can be seen here.

Followers

Blog Archive