ecmascript-6

Why are javascript functions within classes not hoisted?

故事扮演 提交于 2021-02-08 07:24:38
问题 class A { f1() { f2(); } f2() {} } var a = new A(); console.log(a.f1()); returns f2 is not defined. Whereas: { function f1() { return f2(); } function f2() { return 'f2'; } console.log(f1()); } prints 'f2' I'm just wondering why functions within classes are not hoisted? 回答1: class A { f1() { return f2() } f2() { return 'f2' } } var a = new A() console.log(a.f1()) is not equivalent to { function f1() { return f2() } function f2() { return 'f2' } console.log(f1()) } Instead, it is syntactic

Can't call a function from js file imported as type module

♀尐吖头ヾ 提交于 2021-02-08 06:33:37
问题 I am writing my Javascript files in ES6 using module imports. Using type='module' is now supported on most modern browsers to allow for proper parsing of import statements. script type="module https://caniuse.com/#feat=es6-module I built an HTML select element were onchange() calls a function from one of my module files using select onchange="someFunction()" but an error is always thrown saying the function is not defined when the on change event occurs. I tested the same function inline and

Why is Boolean.prototype a Boolean object again? (And same for String and Number, but not Date or RegExp?)

时光怂恿深爱的人放手 提交于 2021-02-08 05:31:10
问题 In ES5, Boolean.prototype is a Boolean object: The Boolean prototype object is itself a Boolean object (its [[Class]] is "Boolean") whose value is false. In ES6 / ES2015, it isn't: The Boolean prototype object is an ordinary object. It is not a Boolean instance and does not have a [[BooleanData]] internal slot. In ES2016, it is once again: The Boolean prototype is itself a Boolean object; it has a [[BooleanData]] internal slot with the value false. (and it remains so in ES2017 as well.) The

What determines the order of `require` calls in babel-transpiled scripts?

狂风中的少年 提交于 2021-02-08 05:02:08
问题 So, my workflow up to this point was to put import "babel-polyfill"; when using features like async / await to ask babel to include the regenerator runtime in the transpilation. I see the the following problems for users requiring my module: The user is in an ES2015 environment and transpiles his code with babel-polyfill , too. Since babel-polyfill can only be required once, he will not be able to use my module at all. If I thus choose not to include babel-polyfill , babel doesn't know that

What determines the order of `require` calls in babel-transpiled scripts?

孤街醉人 提交于 2021-02-08 05:01:08
问题 So, my workflow up to this point was to put import "babel-polyfill"; when using features like async / await to ask babel to include the regenerator runtime in the transpilation. I see the the following problems for users requiring my module: The user is in an ES2015 environment and transpiles his code with babel-polyfill , too. Since babel-polyfill can only be required once, he will not be able to use my module at all. If I thus choose not to include babel-polyfill , babel doesn't know that

how to write a multi lines state with Hooks useState in ReactJs

怎甘沉沦 提交于 2021-02-08 04:31:13
问题 React 16.9 I am aware that this class component state : class JustAnotherCounter extends Component { state = { count: 0 }; is the equivalent of using Hooks useState : function JustAnotherCounter() { const [count, setCount] = useState(0); ..but what would be the equivalent of the class component state below using Hooks useState ?: class Main extends Component { state = { step: 1, firstName: '', lastName: '', jobTitle: '', jobCompany: '', jobLocation: '', } Any help would be much appreciated.

how to write a multi lines state with Hooks useState in ReactJs

泪湿孤枕 提交于 2021-02-08 04:31:09
问题 React 16.9 I am aware that this class component state : class JustAnotherCounter extends Component { state = { count: 0 }; is the equivalent of using Hooks useState : function JustAnotherCounter() { const [count, setCount] = useState(0); ..but what would be the equivalent of the class component state below using Hooks useState ?: class Main extends Component { state = { step: 1, firstName: '', lastName: '', jobTitle: '', jobCompany: '', jobLocation: '', } Any help would be much appreciated.

Move zeroes in array using ES6 features

大城市里の小女人 提交于 2021-02-07 19:18:40
问题 I’m new to ES6, trying to make a function that moves all zeros in the array in the last position of the array while preserving the original order of the array E.g. [1,0,0,0,2,3,4,5] => [1,2,3,4,5,0,0,0] function moveZeros (arr) { let zeroArr = []; for(let i = 0;i < arr.length;i++) { if (arr[i] == 0) { zeroArr.push(arr[i]); arr.splice(i, 1); } } arr.push(...zeroArr); return arr; } This is my code its works fine, but I think this can be shorter more in using some ES6 features. Can someone

How to use call() for inheritance. Error: Class constructor cannot be invoked without 'new'

霸气de小男生 提交于 2021-02-07 14:20:08
问题 Can you explain me how to implement inheritance when using class ? When I use function for defining the constructor, everything works (cf. code version 1). But when I turn function into an ES2015 class (version 2) it produces this error: Uncaught TypeError: Class constructor Person cannot be invoked without 'new' Do I need to add something to the code or should I just leave it as it is with function ? 1. Working code using function function Person(firstName, lastName) { this.firstName =

Passing React context through an HOC to a wrapped component

情到浓时终转凉″ 提交于 2021-02-07 13:58:46
问题 Is there a way that you can pass context through a React higher order component to a the component it wraps? I have a HOC that receives context from its parent and utilizes that context to perform a basic, generalized action and then wraps a child component that also needs to access that same context to perform actions. Examples: HOC: export default function withACoolThing(WrappedComponent) { return class DoACoolThing extends Component { static contextTypes = { actions: PropTypes.object, }