Jan 19, 2022 18:01
If that's at the top level of the file then it's not valid code. Functions need to be declared with the function keyword. So the problem is not with any settings but that you are using the wrong syntax. The method syntax is only valid inside object literals or class declarations.
Jan 19, 2022 18:01
@Owl: Maybe or maybe not. XY problem
Jan 19, 2022 18:01
1) Maybe you have a syntax error in your code. 2) Maybe you are using features that are not supported by the version of Node.js you are using. 3) ... We cannot help you if you don't provide concrete information.
Jan 19, 2022 18:01
Your question is confusing, probably because you are not familiar with the terms yourself. JavaScript is an implementation of ECMAScript and Node.js is a host environment + JavaScript engine.
 
Jan 19, 2022 18:00
@Bergi: Is there? I know of github.com/engine262/engine262 but I don't think it's automatic (and it doesn't actually do what I want). If there is one I'd be curious to take a look at it. However, I have the translation part working, the UI is somehow more challenging...
Jan 19, 2022 18:00
FWIW, I started working on something that would help answer questions like this (basically an executable version of the spec) but it's not ready yet :-/
Jan 19, 2022 18:00
Since b is a variable, 6.1 will be executed: Return ? base.GetBindingValue(GetReferencedName(V), IsStrictReference(V)) . The result will be a number value.
 
Mar 9, 2019 02:32
.push certainly adds a value to an array. See: var foo = [1,2,3]; foo.push(4); console.log(foo);. If "it doesn't work" then things may be different than we understand them and you have to provide more information.
Mar 9, 2019 02:32
If you want to add it statically: Just edit the source code. If you want to do it dynamically, store the config in a variable first and do config.stores.push({...}).
Mar 9, 2019 02:32
It's unclear what your problem is. Please elaborate. What exactly is a pain? What are the methods you tried?
 
Feb 27, 2019 22:39
 
Jul 13, 2018 18:31
I find it confusing that a player contains players. That makes no sense to me. However, if you want the "inner objects" to have the same method as the one you return from Player then just set those: const newPlayer = {id: null, name, symbol, type: playerType, move: null, PlayerType, players, getPlayerById};. But overall this looks like a bad solution to me. You should describe the high level architecture / goal so we can understand better what you want to achieve.
Jul 13, 2018 18:31
"it's not inheriting all the functions I'm returning" which functions? Inheriting from where? Please provide a Minimal, Complete, and Verifiable example. I think what makes it difficult for us to help you is that you are not providing the complete picture.
Jul 13, 2018 18:31
"Trying to figure out how to work with that player = Player(); instance of player by calling the add function to add to the array of that player instance" Can you elaborate on that an provide a clearer and more complete example?
Jul 13, 2018 18:31
This might be a good use case for a class then.
Jul 13, 2018 18:31
@PositiveGuy: OK, but this is not the scope. You cannot access scope from user-land code. this is like an implicitly set parameter.
Jul 13, 2018 18:31
What is the purpose of the Player function anyway? It doesn't seem to represent a single player so the name is already confusion. Should it be called Game instead maybe?
Jul 13, 2018 18:31
There is no instance. If you call the function with new, then this would be an "instance" of the (constructor) function. But I don't see how that makes sense anyway. It looks like addPlayer should always return a new object (btw, Object.assign({}, {...}) is the same as just {...}).
Jul 13, 2018 18:31
In "normal" function calls foo(), this is undefined. It's unclear to me what you expect the value of this to be here.
 
Jun 6, 2018 04:19
"So what does closure mean?" I explained that earlier: "A closure is a function that has a reference to the environment it was created in and it uses that environment to resolve free variables." And an environment is just a mapping of variable names to variable values. One way of implementing global scope (and how it is done in JavaScript) is to have every function be a closure and have the global scope at the end of the scope chain that the function closes over. Because that means that every function has access to that scope.
Jun 6, 2018 04:19
PHP has explicit syntax for creating closures which would look like this: $x = 1; function foo() use ($x) { echo $x; }; foo();. "A lot of what closure is comes from basic inheritance and scope (lexical) as I see it" Yes, kind of. Basically, all the nested lexical scoped are linked. So if a variable is looked up but doesn't exist in one scope, the falls back to looking it up in the "parent scope". But you should not confused that with traditional inheritance in OOP. The scopes are just linked one way or the other.
Jun 6, 2018 04:19
I don't quite understand what your problem with return function() {...} is, but I have provided two examples that demonstrate closure behavior one with return and one without. And if your argument is that the second is not a closure than I have to say: it is. If you consider the same example in PHP: $x = 1; function foo() { echo $x; }; foo(); then you'd get an error, because $x is not defined inside foo. Being a closure allows a function to access variables that are not defined "in itself". That makes the example in JavaScript a closure.
Jun 6, 2018 04:19
Look at the language specification. Step 5 is what enables the closure behavior. If just leave that step out, then functions would not be closures. So, closures are not a consequence of functions being objects, it's an explicit decision that the language designers made, which is independent from making functions objects.
Jun 6, 2018 04:19
"The fact that a function is an object itself implies closure behavior." No, it does not, at all. JavaScript could implement closures without functions being objects and vice versa (i.e. have functions not be closures). Maybe you don't quite know what a closure is: A closure is a function that has a reference to the environment it was created in and it uses that environment to resolve free variables. Nothing in that sentence requires any "objects" (whatever "objects" are in an abstract context).
Jun 6, 2018 04:19
It is a closure. How do you think the function has access to variable x? "all I am saying if functions are objects, defining a new term called closure has no meaning because that is how objects would behave anyways" Closures have nothing to with objects. Closures exist in other languages where functions are not objects, e.g. PHP. It's an abstract programming/language concept. Besides, you are conflating a couple of things: Yes, functions are objects, but when you call new func() then you are creating a new plain object. It has nothing to do with functions being objects.
Jun 6, 2018 04:19
This is just an example. Whether or not I return a function is irrelevant. Whether I use let is irrelevant. Here is an even simpler example: var x = 1; function foo() { console.log(x);}; x = 2; foo();. "always prefer adding an attribute to the DOM" No all event handling has to do with the DOM. Think about Ajax requests...
Jun 6, 2018 04:19
What you have is just a constructor function. That doesn't really having anything to do with closures or the problem they solve.
Jun 6, 2018 04:19
"t is simply returning the function with the variables from the parent scope "plugged in as they were when the return was called." No, free variables are evaluated when the "closure function" is called not when it is created. Simple example var closure = (() => { let x = 0; setTimeout(() => x = 1, 0); return () => console.log(x); })(); setTimeout(closure, 0);
Jun 6, 2018 04:19
tl;dr: How can function access values? Usually as parameters, i.e. the values a function needs are passed to it at call time. That is all good and fine if you have full control how and when a function is called. But in JavaScript functions are very often used as callbacks, i.e. you do not control when and how a function is called. So you need another to provide values that the function needs and the concept of closures does this "naturally". Btw, in theory every function is a closure in JavaScript, whether you use it as such or not.
 
May 9, 2018 01:58
"I changed the capitalization between the original post and when I generated the transpiled code" I hope you understand that any inconsistency is confusing (and possibly wastes our time investigating) because we cannot know whether it's relevant to the problem or not.
May 9, 2018 01:58
But, how come the original code contains origFile but the transpiled code has origfile (lower case f)?
May 9, 2018 01:58
"In both cases it returns an object. Only in the first case does it set the const." That's not possible. An assignment doesn't "fail" unless you are trying to assign to an existing const or a non-existing variable. How do you verify that the value is not set? I don't think we can help you much if you cannot produce a Minimal, Complete, and Verifiable example.
May 9, 2018 01:58
You are wondering why two different code paths produce different results? Look at what's different between the two function calls. Have you verified what the value of action is in both cases?
May 9, 2018 01:58
Is that the full code for the cases? origFile is not visible outside of their respective blocks ({...}), so this code is useless.
 
Mar 31, 2017 18:04
You're welcome!
Mar 31, 2017 17:34
If you just want a subset of the properties and not actually change your state, that doing what I have shown above (`var obj = Object.assign({}, survey[0])`) will create a copy of the object from which you can remove the properties.

Or you could simply build a new object by picking the properties you need (e.g. something like `var data = {foo: survey[0].foo, bar: survey[0].bar};`.
Mar 31, 2017 17:29
That also applies to any other nested values in the state and of course also applies to any mutation operations, not just assignments.
Mar 31, 2017 17:28
It's simply how JavaScript works. Which is why it is explicitly called out in the React documentation to not change the state directly: facebook.github.io/react/docs/…
Mar 31, 2017 17:24
Unless you are creating a copy of the object, there is only this single instance of that object in your application. This object can be referenced from multiple places (state, local variables, etc). But since all of these places reference the same object, any changes to that object will be visible on all these places.
Mar 31, 2017 17:19
E.g. var obj = Object.assign({}, survey[0]) is an easy way to create a shallow copy.
Mar 31, 2017 17:18
Well, you can make a copy of the array. But the more important thing is that you make a copy of the value that you are actually changing, which is the object.
Mar 31, 2017 17:17
s[0] and survey[0] refer to the same object.
Mar 31, 2017 17:17
As I said, that only creates a shallow copy of the array itself, but not the objects within the array.
Mar 31, 2017 17:14
As I have said, you would have to make a copy of the object you are going to change.
Mar 31, 2017 17:14
That only creates an copy of the array itself, but not the objects inside the array. I.e. survey[0] === this.state.survey[0].
Mar 31, 2017 17:14
"because I'm passing a copy of state to it" All I can see is that you are passing this.state.survey, which is not a copy. JavaScript represents objects (arrays are objects too) as references. They are not copied on assignment.
 
Jan 3, 2017 09:17
Number(JSON.parse(valueFromLocalStorage)) should work. But there is no need to use JSON in your case anyway, so why not just do localStorage.setItem("Shopping cart", pastenum ); and Number(localStorage.getItem('Shopping cart'))?
 

JavaScript

Topic: Anything JavaScript, ECMAScript including Node, React, ...
May 9, 2016 16:00
If you are following ECMAScript development, I would appreciate it if you could vote here: twitter.com/fkling42/status/729700943164399616 (the question is: "What do you think "ECMAScript 7" (ES7) stands for. Thanks!
 
Aug 13, 2015 15:57
Again, I'm not saying that what you wrote is wrong, it just feels a bit incomplete :)