Skip to content

Instantly share code, notes, and snippets.

@othree
Last active February 10, 2017 18:17
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save othree/b313500e93d37b6606e8315ba51459b8 to your computer and use it in GitHub Desktop.
Save othree/b313500e93d37b6606e8315ba51459b8 to your computer and use it in GitHub Desktop.

Secret ECMAScript syntax

https://tc39.github.io/ecma262/

const

Also block scoped, like let.

Null Propagation Operator

var obj;
obj.foo.bar;

ReferenceError

obj ? (obj.foo ? obj.foo.bar : null) : null;
obj?.foo?.bar;

Called Existential Operator in CoffeeScript
http://coffeescript.org/#existential-operator

Stage 0 to 1
https://claudepache.github.io/es-optional-chaining/

Async Function

async function xd () {
  // await something
}

Also supports arrow function

async () => {}
async arg => {}

But async is not keyword

var async = 1;
var async = async function async () {};

await is keyword.

Template String

`string content`

CAN HAVE NEW LINE!!

`string
content`

supports customized tag function

function tag() {console.log(arguments);}

var foo = 'a';

tag`ha ${foo}`;

String.raw https://developer.mozilla.org/zh-TW/docs/Web/JavaScript/Reference/Global_Objects/String/raw

String.raw({raw:['xd', 'dx']}, '--');

Class

Can be anonymous

var A = class {};
export default class {};

Can extend

class B extends A {}

Can extend with new

class B extends new A {}

A need to be a constructor

In fact, extends followed by a LHS expression

function tag () {return function () {};}

class B extends tag`haha` {}

Property Name

can be expression or literal

class C {
  ['1']() {}
}
class D {
  2() {}
}

or keyword

class E {
  class() {}
}

Thats why React need to use className

Method Definition

Can used to declare an object

var f = {
  method() {}
}

Requires , to add another property

var g = {
  method() {},
  prop: 1
}

But not able to have , in class

Use ; instead if you really want ,

class G {
  class() {};
  method() {}
}

Comment

  • Singleline
  • Multiline

and

  • HTML comment
<!-- -->

https://tc39.github.io/ecma262/#prod-annexB-Comment

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment