TOGOUTECH

es6-class

全部标签

javascript - 有没有更好的方法在 React Component 类中绑定(bind) 'this'?

我目前正在开发一个React应用程序,我发现当一个组件类有很多功能时必须绑定(bind)this有点麻烦。例子classFooextendsComponent{constructor(props){super(props);this.function1=this.function1.bind(this);this.function2=this.function2.bind(this);this.function3=this.function3.bind(this);}function1(){...}function2(){...}function3(){...}}有没有更有效的方法来做

javascript - "Class extends value #<Object> is not a constructor or null"

感谢阅读我的文章我的代码出现此错误:“Classextendsvalue#isnotaconstructorornull”这是我的代码,我正在尝试导出/导入类。怪物.js:constminiMonster=require("./minimonster.js");classmonster{constructor(options={name},health){this.options=options;this.health=100;this.heal=()=>{return(this.health+=10);};}}letbigMonster=newmonster(&

javascript - Getter/Setter 和原型(prototype)链

这个问题在这里已经有了答案:Overrideasetter,andthegettermustalsobeoverridden(1个回答)关闭3年前。在当前使用ES6类语法和get/set语法的JavaScript项目中,我偶然发现了一个我无法解释的行为。首先,一个按预期工作的提取演示:classA{constructor(){this.__value=null;}getvalue(){returnthis.__value;}setvalue(value){this.__value=value;}}classBextendsA{}letb=newB();b.value=2;console

javascript - 为什么在构造函数中直接创建 ES6 类的实例时 Jest 的 toThrow 不起作用?

classTestObject{constructor(value){if(value===null||value===undefined){thrownewError('Expectavalue!');}}}describe('testtheconstructor',()=>{test('itworks',()=>{expect(()=>{newTestObject();}).toThrow();});test('notwork',()=>{expect(newTestObject()).toTh

javascript - 如何在 ES6 类中使用静态变量?

我正在尝试在es6中使用静态变量。我想在Animal类中声明一个静态变量count并增加它。但是,我无法通过staticcount=0;声明静态变量,所以我尝试了另一种方式:classAnimal{constructor(){this.count=0;}staticincreaseCount(){this.count+=1;}staticgetCount(){returnthis.count;}}console.log(Animal.increaseCount());//undefinedconsole.log(Animal.getCount());//NaN我希望console.lo

javascript - 人们更喜欢使用 React 组件的构造函数而不是 componentWillMount 有什么原因吗?

我发现使用生命周期方法componentWillMount来设置初始状态...componentWillMount(){this.state={comments:[]};}...比使用构造函数稍微简单一些。即因为当您使用构造函数时,您有调用super()。constructor(){super();this.state={comments:[]};}不仅如此,如果您的组件传递了props和/或state,那么您还必须手动传递它们。constructor(props,state){super(props,state);...}我在使用componentWillMount时没有遇到任何问题

javascript - 扩展 Javascript promise 并在构造函数中解决或拒绝它

我想用ES6语法扩展原生JavascriptPromise类,并且能够在子类构造函数中调用一些异步函数。根据异步函数结果,promise必须被拒绝或解决。然而,当then发生了两件奇怪的事情。函数被调用:子类构造函数执行两次“UncaughtTypeError:Promiseresolveorrejectfunctionisnotcallable”抛出错误classMyPromiseextendsPromise{constructor(name){super((resolve,reject)=>{setTimeout(()=>{resolve(1)},1000)})this

javascript - 在没有构造函数的情况下 react 定义状态

这个问题在这里已经有了答案:initstatewithoutconstructorinreact(3个答案)关闭5年前。我见过一些React开发人员在没有构造函数的情况下定义状态。我喜欢这样可以简化代码,但这样做安全吗?classDogextendsReact.Component{state={sound:'Woof'}return(<p>Dogsays{this.state.sound}</p>)}对于过于简化的示例,我提前表示歉意。

javascript - React 中的 Constructor(props) 和 super(props) VS constructor() 和 super()

我知道这个问题被问过很多次了,但还是不清楚。很多人只是说:Passpropstoconstructorifyouwanttoaccessthis.propsthereonemoreexampleoftheanswer官方文档说Classcomponentsshouldalwayscallthebaseconstructorwithprops.,但如果我们不将props传递给constructor,除了构造函数之外,我们仍然会在任何地方都有this.props。同样来自reactsourcecode我们可以看到React.Component的源代码函数ReactComponent(pro

javascript - ECMAScript 6 类属性下划线前缀

我见过的类模式几乎都是这样的:classFoo{constructor(x,y,z){this._x=x;this._y=y;this._z=z;}getx(){returnthis._x;}setx(value){//Iacctuallydosomestuffherethis._x=value;}gety(){returnthis._y;}sety(value){//Iacctuallydosomestuffherethis._y=value;}getz(){returnthis._z;}setz(value){//Iacctuallydosomestuffherethis._z=v