es6-class

Extend Javascript promise and resolve or reject it inside constructor

徘徊边缘 提交于 2019-12-12 10:38:54
问题 I want to extend native Javascript Promise class with ES6 syntax, and be able to call some asynchronous function inside the subclass constructor. Based on async function result the promise must be either rejected or resolved. However, two strange things happen when then function is called: subclass constructor is executed twice "Uncaught TypeError: Promise resolve or reject function is not callable" error is thrown class MyPromise extends Promise { constructor(name) { super((resolve, reject)

Node ES6 class event emitter function?

微笑、不失礼 提交于 2019-12-12 06:38:05
问题 I'm trying to create a class for which all instances respond to an event: const events = require("events"); const eventEmitter = new events.EventEmitter(); class Camera { constructor(ip) { this.ip = ip; } eventEmitter.on("recordVideo", recordClip); recordClip() { console.log("running record video"); } } // emit event once a minute setInterval(function(){ eventEmitter.emit('recordVideo'); }, 1000*60); The recordClip function never seems to be called. Is this possible? I also tried running this

ES6: Super class doesn't hold state

假如想象 提交于 2019-12-12 05:31:57
问题 I'm trying to figure out what's going on here, as the Parent/Super class does not have data after the initial construction. // imports/server/a-and-b.js class A { constructor(id) { // make MongoDB call and store inside this variable // ... this._LocalVariable = FieldFromMongo; console.log(`this._LocalVariable: ${this._LocalVariable}`); // => This has a good value, ie: 'Test' } get LocalVar() { console.log(`this._LocalVariable: ${this._LocalVariable}`); // => This has a undefined value when

How to add event listener and move an object in canvas using ES6 Classes?

戏子无情 提交于 2019-12-11 19:35:38
问题 I'm creating a small game where you can move a star/ufo. I'm having a hard time to figure out how I should make it move. With functional programming its easy, but how do we go about doing this with ES6 utlizing classes? Do we need to bind or something? I suppose my logic is also somewhat wrong. How can I make the circle move? Codepen: https://codepen.io/Aurelian/pen/mGWVbq?editors=0010 'use strict'; /* * ------------------------------------------ * *----------------------------- * User Event

Can ES6 constructors be stubbed more easily with Sinon?

丶灬走出姿态 提交于 2019-12-11 18:37:15
问题 Given the (overly simplified) snippet: import Validator from 'validator'; export default function isValid(arg) { // Validator#isValid is an ES6 getter return new Validator(arg).isValid; } How can I test that a Validator was instantiated with the given parameter? And stub isValid ? I know I can restructure my code to avoid the issue, I am not looking for a workaround as I found plenty (dependency injection, not using ES6 sugar, etc.). I found a way, but it is terribly ugly. In test file:

Does super() map to __proto__ under the hood?

老子叫甜甜 提交于 2019-12-11 18:27:53
问题 I understand that classes in ES6 are really syntactic sugar. Is the super() call really just calling proto ? (Is it mapped to the [[prototype]] object?) 回答1: It's a bit more than that. It also remembers where the method was defined. const example = { method() { return super.method(); } } is syntactic sugar for const example = { method() { return Object.getPrototypeOf(example).method.call(this); } } and class Example { method() { return super.method(); } } is syntactic sugar for class Example

Why can't Javascript ES6 call an anonymous super function? [duplicate]

こ雲淡風輕ζ 提交于 2019-12-11 17:35:38
问题 This question already has an answer here : Accessing a class field on a superclass (1 answer) Closed 4 months ago . Please explain the difference between using a parent-child relationship using anonymous functions and using class functions? In case 1 everything works as expected. In case 2, codepen does not return any results. //CASE 1 class Parent { constructor(name) { this.name = name; } exec() { console.log('name', this.name); } } class Child extends Parent { constructor(name, age) { super

Why parentheses (grouping operator) aren't needed to call a “get method” through an instance?

孤人 提交于 2019-12-11 17:34:17
问题 I am reading about JavaScript Classes at MDN reference, and see an example where a method is defined using a get keyword. Here, I noticed that no parentheses (grouping operator () ) are required to call such a method (defined using get keyword) through an instance of the class. Like, in following example, square.area syntax calls the Rectangle Class's area method. However, square.area() throws an error Uncaught TypeError: square.area is not a function . Can someone please explain what am I

What's different between two ways of defining React Component?

这一生的挚爱 提交于 2019-12-11 15:43:49
问题 There're 2 ways to define a React component. First one is like below. class SomeComponent extends React.Component { constructor (props) { super(props) this.state = { someState: false } this._handleOnChangeState = this._handleOnChangeState.bind(this) } _handleOnChangeState (e) { this.setState({ someState: e.target.value }) } .... } Second one is like below. class SomeComponent extends React.Component { state = { someState: false } _handleOnChangeState = (e) => { this.setState({ someState: e

exporting React component class with one name, importing it with another name, still working. Why?

隐身守侯 提交于 2019-12-11 14:18:13
问题 here is one file ( clock.js ) for my React app class Clock extends Component { ... ...} export default Clock; here's another where I'm importing the Clock component import Clock_1 from './clock/clock'; ... <Route exact path="/clock" component={Clock_1} /> as you can see, I exported it with name Clock and imported it with name Clock_1, still it is compiling and running successfully. Why? PS: Apologies beforehand if this question sounds lame/ too simple/ stupid. I'm a beginner. 回答1: [ES6