ecmascript-6

Destructure an array parameter [duplicate]

限于喜欢 提交于 2021-02-04 21:17:31
问题 This question already has an answer here : Syntax for destructuring arrays into function parameters in ES6 (1 answer) Closed 1 year ago . Is it possible to destructure a function parameter? For example, I would want to convert this: Object.entries({}).find(group=> group[0] === "foo" && group[1] === "bar"); To something like this: Object.entries({}).find([0: key, 1: value] => key === "foo" && value === "bar"); or Object.entries({}).find([...key, value] => key === "foo" && value === "bar"); 回答1

How to import a static member of a class?

自闭症网瘾萝莉.ら 提交于 2021-02-04 19:49:14
问题 I am trying to import a static member of a class into a file by just using standard import syntax. To give context: Destructuring works on the static method of a class: class Person { static walk() { console.log('Walking'); } } let {walk} = Person; console.log(walk); // walk function However, I thought imports behaved like destructuring assignments. If that's true, then I would expect the following to work. But, when I attempt to import the walk method, it just comes back as undefined :

If babel converts let and const to var, what's the difference?

大憨熊 提交于 2021-02-04 19:16:08
问题 I've tried the babel transpiler, and it converts All let, const and var to just var, so in all, what's the difference in our code usage? I have read documents and I know what's the difference between let, const and var , but if all of them are eventually converted to var , what's the difference ? it means that there shouldn't be any meaningful differences in performance or even scope! Update (02.14.2019) : Based on the answers I understood that scope does matter and even though they are

Extracting properties out of an object natively

懵懂的女人 提交于 2021-02-04 19:12:05
问题 I use underscore.js library to extract properties out of an object. Is there a more native JS way to accomplish the same: var fullObject = {'name': 'Jack', 'age': 39, 'device': 'tablet', 'team': 'Red'} const {name, device, team} = fullObject console.log(name, device, team) // Jack tablet Red Is there a way to create a new object through destructuring? I would like to assign the values of name , device , team to a new object. Currently I do: const {name, device, team} = fullObject const

Property 'value' does not exist on type 'never'. when use useRef hook in mui

梦想的初衷 提交于 2021-02-04 18:18:31
问题 I am using material UI to build a login and registration page, using useRef to return a TextFiled ref instance, and xxxRef.current.value to get the input value. I can smoothly run my project and can get the value correctly,but the console always reminded me that: Property 'value' does not exist on type 'never'. Here is my code snippets: const accountRef = useRef(); <TextField variant="outlined" margin="normal" required fullWidth id="account" label="Account" name="account" autoComplete=

Eslint rule to put a new line inside import

旧城冷巷雨未停 提交于 2021-02-04 14:51:13
问题 The rule that I'm looking should show error in this case: import {MY_CONSTANT1, MY_CONSTANT2, MY_CONSTANT3} And considered as fine in this case: import { MY_CONSTANT1, MY_CONSTANT2, MY_CONSTANT3 } Is there such eslint rule? 回答1: I was looking for such a rule for both import and export declaration. As a result I've made a plugin with autofix. So plugin transforms the code import { k1, k2 } from 'something' into import { k1, k2 } from 'something' and code export { name1, name2, nameN } into

Eslint rule to put a new line inside import

点点圈 提交于 2021-02-04 14:51:05
问题 The rule that I'm looking should show error in this case: import {MY_CONSTANT1, MY_CONSTANT2, MY_CONSTANT3} And considered as fine in this case: import { MY_CONSTANT1, MY_CONSTANT2, MY_CONSTANT3 } Is there such eslint rule? 回答1: I was looking for such a rule for both import and export declaration. As a result I've made a plugin with autofix. So plugin transforms the code import { k1, k2 } from 'something' into import { k1, k2 } from 'something' and code export { name1, name2, nameN } into

map function not working in React

怎甘沉沦 提交于 2021-02-04 14:02:03
问题 I am new to React JS, I was testing out some functions in fiddler. I am not sure why I get an error pointing to the map function. I am not able to render the array i defined. Relevant snippet: {this.props.data.productSpecs.map(function(productSpec){ <b>Category Name:</b> {productSpec}; })} Full code: var productCategory = { productName: 'SamamgaTV1', productCategory: 'Television', productSpecs: ['32inch','black','hd'] }; var ProductComponent = React.createClass({ render: function() { return(

Why does const work in some for-loops in JavaScript?

核能气质少年 提交于 2021-02-04 10:08:10
问题 I do know why const doesn't work in for-loops. We need to create a new scope and copy over a value into that. So this won't fly. for(const i = 0; i < 5; i++) console.log(i); Whereas this will. for(let i = 0; i < 5; i++) console.log(i); However, I noticed that both of them work when looping though the properties of an object like this. for(let property in thingy) console.log(property); for(const property in thingy) console.log(property); I'm not sure why. 回答1: for (const property in object)

Why does const work in some for-loops in JavaScript?

南楼画角 提交于 2021-02-04 10:04:03
问题 I do know why const doesn't work in for-loops. We need to create a new scope and copy over a value into that. So this won't fly. for(const i = 0; i < 5; i++) console.log(i); Whereas this will. for(let i = 0; i < 5; i++) console.log(i); However, I noticed that both of them work when looping though the properties of an object like this. for(let property in thingy) console.log(property); for(const property in thingy) console.log(property); I'm not sure why. 回答1: for (const property in object)