Tag: es2015

React.js ES6避免将“this”绑定到每个方法

最近,我开始修补React.js,我喜欢它。 我开始在常规的ES5中,以便弄清事情,这些文档都是用ES5编写的。 但是现在我想尝试ES6,因为它有光泽,新颖,而且似乎简化了一些东西。 我感到困扰的是,对于我添加到组件类中的每种方法,我现在都必须绑定“this”,否则它不起作用。 所以我的构造函数最终看起来像这样: constructor(props) { super(props); this.state = { …some initial state… } this.someHandler = this.someHandler.bind(this); this.someHandler = this.someHandler.bind(this); this.someHandler = this.someHandler.bind(this); this.someHandler = this.someHandler.bind(this); this.someHandler = this.someHandler.bind(this); this.someHandler = this.someHandler.bind(this); this.someHandler = this.someHandler.bind(this); } 如果我要给class上添加更多的方法,这将会变得更大,更丑陋。 我的问题是,有没有办法解决这个问题,或者至less让它更容易,更短,更难看? 我想尝试使用ES6的一个主要原因是为了使我的代码更加简洁,但是正好相反。 任何build议或意见,将不胜感激。

JavaScript ES6类中的私有属性

是否有可能在ES6类中创build私有属性? 这是一个例子。 我怎样才能防止访问instance.property ? class Something { constructor(){ this.property = "test"; } } var instance = new Something(); console.log(instance.property); //=> "test"

bower init – amd,es6,globals和node之间的区别

我正在创build我的第一个Bower组件。 在运行bower init ,脚本问我“这个软件包暴露了什么types的模块? 与这些选项: AMD ES6 全局 节点 这些选项有什么区别?