是否可以在ES6类中创建私有属性?
这是一个例子.我怎样才能阻止访问instance.property?
class Something {
constructor(){
this.property = "test";
}
}
var instance = new Something();
console.log(instance.property); //=> "test"
Run Code Online (Sandbox Code Playgroud) 我正在创建我的第一个Bower组件.运行bower init脚本后问我'这个包暴露了什么类型的模块?' 有这些选项:
这些选项有什么区别?
最近,我开始修补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);
}
Run Code Online (Sandbox Code Playgroud)
如果我要为我的课程添加更多的方法,这将成为一个更大,更丑陋的混乱.
我的问题是,有没有办法解决这个问题,或者至少让它变得更容易,更短,更难看?我想尝试与ES6一起使用React的主要原因之一是让我的代码更简洁,但这恰恰相反.任何建议或意见将不胜感激.
在一个真实的简单案例中测试它们会产生相同的输出:
const obj = {a: 5, b: 5};
console.log(Reflect.ownKeys(obj));
console.log(Object.keys(obj));
// Result
['a', 'b']
['a', 'b']
Run Code Online (Sandbox Code Playgroud)
什么时候Reflect.ownKeys(obj)产生的输出不同Object.keys(obj)?
我有两个javascript模块,如下所示:
// inner/mod.js
export function myFunc() {
// ...
}
// mod.js
import * as inner from "./inner/mod";
Run Code Online (Sandbox Code Playgroud)
我想出口myFunc的mod.js.我怎样才能做到这一点?
编辑:我应该澄清该功能正在按预期导出,inner/mod.js但我也想从外部导出功能mod.js.
对于那些要求澄清的人,我想实现这个目标:
// SomeOtherFile.js
import * as mod from "mod"; // NOT inner/mod
mod.myFunc();
Run Code Online (Sandbox Code Playgroud) 我有一个带有getter属性的ES6类(用babeljs反编译).我知道默认情况下这些属性不可枚举.但是,我不明白为什么我无法使用可枚举的属性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 and get enumerable properties
var person = new Person('Billy');
var enumerableProperties = Object.keys(person);
// => ['name']
Run Code Online (Sandbox Code Playgroud)
我在上一个项目中开始使用es2015和babel.当我尝试做import或export在if条件内时,我有一个错误'import' and 'export' may only appear at the top level.我看到了很多这样的情况require,但它与es2015模块配合使用效果不错.这种限制有什么理由吗?
我想知道以下是否符合ES6规范:
class X {
constructor(name) {
this._name = name;
}
get name() {
return this._name;
}
set name(name) {
this._name = name + "X";
}
}
class Y extends X {
constructor(name) {
super(name);
}
set name(name) {
super.name = name;
this._name += "Y";
}
}
Run Code Online (Sandbox Code Playgroud)
这个想法let y = new Y(""); y.name = "hi"应该y.name === "hiXY"是真实的.
据我所知,这在启用了ES6标志的Chrome中无效.它也不能使用Babel与es2015旗帜.是用super.name = ...在继承二传手不是ES6规范的一部分?或者这是Babel实施中的一个错误?
你如何在ES6中编写对象childContextTypes?
var A = React.createClass({
childContextTypes: {
name: React.PropTypes.string.isRequired
},
getChildContext: function() {
return { name: "Jonas" };
},
render: function() {
return <B />;
}
});
Run Code Online (Sandbox Code Playgroud) 我想在ES6中编写我的猫鼬模型.module.exports尽可能基本上替换和其他ES5的东西.这就是我所拥有的.
import mongoose from 'mongoose'
class Blacklist extends mongoose.Schema {
constructor() {
super({
type: String,
ip: String,
details: String,
reason: String
})
}
}
export default mongoose.model('Blacklist', Blacklist)
Run Code Online (Sandbox Code Playgroud)
我在控制台中看到此错误.
if (!('pluralization' in schema.options)) schema.options.pluralization = this.options.pluralization;
^
TypeError: Cannot use 'in' operator to search for 'pluralization' in undefined
Run Code Online (Sandbox Code Playgroud) ecmascript-6 ×10
es2015 ×10
javascript ×9
babeljs ×3
node.js ×2
reactjs ×2
bower ×1
class ×1
js-amd ×1
mongoose ×1