es6-class

Creating custom element without using class keyword

时光怂恿深爱的人放手 提交于 2019-12-04 18:39:00
问题 This is actually more a question about the object-orientation model in ES6. However I am going to use the creation of a new custom element as an example. So the new and shiny (as of today) method to create a new custom element is via customElements.define() which take in a tag name , a constructor , and options (which is optional) according to MDN, Google, and of course the spec. All the documentation listed uses a variation of the new class keyword for constructor . Assuming I don't like the

Returning ES6 Proxy from the ES6 class constructor

痞子三分冷 提交于 2019-12-04 16:25:52
问题 I want user to only set specific properties to an object but as the same time that object should be constructed from custom class. For example var row = new Row({ name : 'John Doe', email : 'uhiwarale@gmail.com' }, Schema); row can have methods. But when user is trying to set row.password , they are not allowed. One way to do it is using new Proxy instead of new Row but then we will loose all cool things we are doing inside Row class. I want new Row to return a proxy object with this

Why cant react set initial state based on props

守給你的承諾、 提交于 2019-12-04 16:02:00
问题 I have a es6 react component that I want the initial value of the state to depend on that of the passed down prop, but its value is always false: AttachStateToProps component <AttachStateToProps VALUE=false /> AttachStateToProps component: class AttachStateToProps extends React.Component { state = { stateValue: this.props.VALUE, } render() { console.log('Value of Prop - ', this.props.VALUE) console.log('Value of State - ', this.state.stateValue) return null } } Every time the value of the

how to define a static property in the ES6 classes [duplicate]

你。 提交于 2019-12-04 09:54:32
问题 This question already has answers here : ES6 Classes - Updating Static Properties (3 answers) Closed last year . I want to have a static property in an ES6 class. This property value is initially an empty array. class Game{ constructor(){ // this.cards = []; } static cards = []; } Game.cards.push(1); console.log(Game.cards); How can I do it? 回答1: One way of doing it could be like this: let _cards = []; class Game{ static get cards() { return _cards; } } Then you can do: Game.cards.push(1);

When is it appropriate to use a constructor in REACT?

ⅰ亾dé卋堺 提交于 2019-12-03 17:03:24
I understand the concept of constructors in OOP languages like C++. However, I am not entirely sure when to use a constructor in REACT. I do understand that JavaScript is object oriented, but I am not sure what the constructor is actually 'constructing'. When rendering a child component, do you need a constructor in the child component? For example: class App extends React.Component { constructor(props) { super(props); this.state = { items: [], error: null } } render () { return ( <React.Fragment> <ChildComponent data={this.state.items}></ChildComponent> </React.Fragment> ) } } I will keep the

Creating custom element without using class keyword

亡梦爱人 提交于 2019-12-03 12:49:50
This is actually more a question about the object-orientation model in ES6. However I am going to use the creation of a new custom element as an example. So the new and shiny (as of today) method to create a new custom element is via customElements.define() which take in a tag name , a constructor , and options (which is optional) according to MDN , Google , and of course the spec . All the documentation listed uses a variation of the new class keyword for constructor . Assuming I don't like the new class syntax, and considering for most part class is a syntatic sugar (according to this

Custom Array-like getter in JavaScript

守給你的承諾、 提交于 2019-12-03 12:09:40
I have a simple ES6 class, like so: class Ring extends Array { insert (item, index) { this.splice(index, 0, item); return this; } } I want to make it so that the indexing for Ring objects wraps, so that new Ring(1, 2, 3)[3] returns 1, new Ring(1, 2, 3)[-1] returns 3, and so on. Is this possible in ES6? If so, how would I implement it? I've read about proxies, which allow a completely customized getter, but I can't figure out how to apply a proxy to a class. I did manage this: var myRing = new Proxy (Ring.prototype, { get: function (target, name) { var len = target.length; if (/^-?\d+$/.test

How and why would I write a class that extends null?

◇◆丶佛笑我妖孽 提交于 2019-12-03 06:28:13
问题 JavaScript's class syntax, added in ES6, apparently makes it legal to extend null: class foo extends null {} Some Googling reveals that it was suggested on ES Discuss that such declarations be made an error; however, other commenters argued for them to be left legal on the basis that someone might want to create a class that has a {__proto__: null} prototype and that side of the argument ultimately prevailed. I can't make much sense of this hypothetical use case. For one thing, while the

how to define a static property in the ES6 classes [duplicate]

安稳与你 提交于 2019-12-03 04:22:36
This question already has answers here : ES6 Classes - Updating Static Properties (3 answers) I want to have a static property in an ES6 class. This property value is initially an empty array. class Game{ constructor(){ // this.cards = []; } static cards = []; } Game.cards.push(1); console.log(Game.cards); How can I do it? One way of doing it could be like this: let _cards = []; class Game{ static get cards() { return _cards; } } Then you can do: Game.cards.push(1); console.log(Game.cards); You can find some useful points in this discussion about including static properties in es6. class Game{

How and why would I write a class that extends null?

老子叫甜甜 提交于 2019-12-02 19:55:53
JavaScript's class syntax , added in ES6, apparently makes it legal to extend null : class foo extends null {} Some Googling reveals that it was suggested on ES Discuss that such declarations be made an error; however, other commenters argued for them to be left legal on the basis that someone might want to create a class that has a {__proto__: null} prototype and that side of the argument ultimately prevailed. I can't make much sense of this hypothetical use case. For one thing, while the declaration of such a class is legal, it seems that instantiating a class declared in this way isn't.