es2015

React.js ES6 avoid binding 'this' to every method

≡放荡痞女 提交于 2019-11-28 04:37:11
Recently, I've started tinkering with React.js and I love it. I started out in the regular ES5, so as to get the hang of things, the docs are all written in ES5... But now I wanted to try ES6, because it's shiny and new, and it does seem to simplify some things. What bothers me a lot is that for every method I had added into my component classes I now have to bind 'this' to, otherwise it doesn't work. So my constructor ends up looking like this: constructor(props) { super(props); this.state = { ...some initial state... } this.someHandler = this.someHandler.bind(this); this.someHandler = this

Why must export/import declarations be on top level in es2015?

拈花ヽ惹草 提交于 2019-11-27 03:10:11
问题 I started using es2015 with babel in last project. When I try to do import or export inside if condition, I have an error 'import' and 'export' may only appear at the top level . I see a lot of cases for that and it works good with require , but not with es2015 modules. Is there any reason for this limitation? 回答1: JavaScript performs static analysis on ES6 modules. This means you cannot dynamically perform imports or exports. Read section 4.2 of this article for more information: A module's

Setting an ES6 class getter to enumerable

风格不统一 提交于 2019-11-27 02:05:39
问题 I have an ES6 class (transcompiled with babeljs) with a getter property. I understand that these properties are not enumerable by default. However, I do not understand why I am not able to make the property enumerable using Object.defineProperty // Declare class class Person { constructor(myName) { this.name = myName; } get greeting() { return `Hello, I'm ${this.name}`; } } // Make enumerable (doesn't work) Object.defineProperty(Person, 'greeting', {enumerable: true}); // Create an instance

React.js ES6 avoid binding 'this' to every method

落爺英雄遲暮 提交于 2019-11-27 00:33:10
问题 Recently, I've started tinkering with React.js and I love it. I started out in the regular ES5, so as to get the hang of things, the docs are all written in ES5... But now I wanted to try ES6, because it's shiny and new, and it does seem to simplify some things. What bothers me a lot is that for every method I had added into my component classes I now have to bind 'this' to, otherwise it doesn't work. So my constructor ends up looking like this: constructor(props) { super(props); this.state =

Private properties in JavaScript ES6 classes

瘦欲@ 提交于 2019-11-25 22:58:02
问题 Is it possible to create private properties in ES6 classes? Here\'s an example. How can I prevent access to instance.property ? class Something { constructor(){ this.property = \"test\"; } } var instance = new Something(); console.log(instance.property); //=> \"test\" 回答1: Private fields are being implemented in the ECMA standard. You can start using them today with babel 7 and stage 3 preset. See babel REPL example. class Something { #property; constructor(){ this.#property = "test"; } }