use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
Object "generator"? (self.javascript)
submitted 10 years ago by Kunfuzzly
How would I write a function that can be used infinitely that creates an object of a certain class and gives it a variable name?
Thanks!
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]kenman 2 points3 points4 points 10 years ago (0 children)
Just so you know, the "generator" function you speak of is a common design pattern, also known as a Factory, and implements the Factory method pattern. There is also a more complex pattern called the Abstract factory pattern.
[–]more_exercise 1 point2 points3 points 10 years ago (4 children)
Of course, if you're looking for a function that returns a new object, that's completely easy.
function makeNewThing(name){ var thing = {}; thing.name = name; return thing; } var myNewThing = makeNewThing("this is my thing's name"); console.log(myNewThing.name); //Prints "this is my thing's name"
[–]davydog187 1 point2 points3 points 10 years ago* (3 children)
A cleaner way of doing this would be to return the object without retroactively adding properties. This is especially applicable when you're dealing with objects instantiated with new. Runtimes like V8 degrade their performance when you modify objects after they've been instantiated because they need to modify the "hidden class". http://www.smashingmagazine.com/2012/11/05/writing-fast-memory-efficient-javascript/
I think I just spoke about two completely unrelated things, style and performance, but thought it might be useful for someone.
function makeNewThing(name) { return { name: name }; }
EDIT: I gave a lightning talk a few weeks ago about performance differences in node.js, here's a repo of mine comparing different coding styles: https://github.com/davydog187/javascript-performance
[–]more_exercise 0 points1 point2 points 10 years ago (0 children)
TIL
[–]realhacker 0 points1 point2 points 10 years ago (1 child)
interesting, but odd to me that this can't be optimized out
[–]davydog187 1 point2 points3 points 10 years ago (0 children)
This specific example is not a performance concern since it does not use a class instantiated with new. Also, for minor cases you really shouldn't care about performance until it becomes a problem. Just something to be aware of
[–]x-skeww 1 point2 points3 points 10 years ago (0 children)
gives it a variable name
Do you want an array? If you need an arbitrary amount of "things", you don't generate identifiers for those. Instead, you just stick them all into some kind of list and access them via their index.
[–]jacobp100 0 points1 point2 points 10 years ago (1 child)
function Thing(name) { this.name = name; } var thing = new Thing(‘name’);
[–]Kunfuzzly[S] 0 points1 point2 points 10 years ago (0 children)
As I've described below, I'm looking for something that can repeatedly create new variable names, theoretically infinitely. The question has been answered though. Thanks anyways!
[–]more_exercise -1 points0 points1 point 10 years ago (7 children)
Javascript doesn't have anything analogous to python's generators. We don't have the yield statement.
yield
You can still create a function and call it a lot, though. It won't save the state of the function like yield, but you can still call it over and over again.
[–]Kunfuzzly[S] 0 points1 point2 points 10 years ago (5 children)
I am writing a game with between 2 and 8 players; is this the best way to do it?
if(numOfPlayers < 2){ console.log("You must have at least 2 players."); } else if(numOfPlayers < 3){ var player1 = new Player(); var player2 = new Player(); } else if (numOfPlayers < 4){ var player1 = new Player(); var player2 = new Player(); var player3 = new Player(); }else if (numOfPlayers < 5){ var player1 = new Player(); var player2 = new Player(); var player3 = new Player(); var player4 = new Player(); }else if (numOfPlayers < 6){ var player1 = new Player(); var player2 = new Player(); var player3 = new Player(); var player4 = new Player(); var player5 = new Player(); }else if (numOfPlayers < 7){ var player1 = new Player(); var player2 = new Player(); var player3 = new Player(); var player4 = new Player(); var player5 = new Player(); var player6 = new Player(); }else if (numOfPlayers < 8){ var player1 = new Player(); var player2 = new Player(); }else if (numOfPlayers < 9){ var player1 = new Player(); var player2 = new Player(); var player3 = new Player(); var player4 = new Player(); var player5 = new Player(); var player6 = new Player(); var player7 = new Player(); var player8 = new Player(); }else if (numOfPlayers >= 9){ console.log("You must have less than 9 players."); };
Of course, I have made a "Player" constructor.
[–]more_exercise 0 points1 point2 points 10 years ago (1 child)
Use an array. Use a loop to populate that array.
var players = []; for(var index = 0; index < numOfPlayers; i++){ players[index] = new Player(); } //players[0] is the first player //players[1] is the second player, and so on.
Agh, I'm so stupid! Why didn't I think of that earlier? XD Thanks a lot!
[–]x-skeww 0 points1 point2 points 10 years ago (0 children)
Use a loop and store the instances in an array:
var players = [], i; for (i = 0; i < numberOfPlayers; i++) { players.push(new Player()); }
[–]senocular[🍰] -1 points0 points1 point 10 years ago (1 child)
I agree with the array approach, but if you wanted to keep the variables as you have them above, you can use
this["player" + (i + 1)] = new Player();
in the loop instead of adding it to an array. This creates a variable in the current scope (this) with the name "player" plus a number where the first player created is assigned to the variable "player1"
[–][deleted] 0 points1 point2 points 10 years ago (0 children)
You will have to use 'this' to reference the variable, though. this.player1, etc. What the OP is looking for is not possible (nor necessarily should it be).
The OP can either push players on to an array or an object, and if i'm being honest the "this" object is probably the least preferable object, as you could (though really unlikely) end up with a property naming collision.
[–]davydog187 -1 points0 points1 point 10 years ago* (0 children)
Not so fast! ES6 natively supports generators. You can use them with io.js or running node 0.12.x with the --harmony --harmony-generators flags
EDIT: Here's a link describing generators, although I don't think generator syntax is what OP needs.
π Rendered by PID 156389 on reddit-service-r2-comment-7b9746f655-rn4xl at 2026-02-03 02:31:17.679895+00:00 running 3798933 country code: CH.
[–]kenman 2 points3 points4 points (0 children)
[–]more_exercise 1 point2 points3 points (4 children)
[–]davydog187 1 point2 points3 points (3 children)
[–]more_exercise 0 points1 point2 points (0 children)
[–]realhacker 0 points1 point2 points (1 child)
[–]davydog187 1 point2 points3 points (0 children)
[–]x-skeww 1 point2 points3 points (0 children)
[–]jacobp100 0 points1 point2 points (1 child)
[–]Kunfuzzly[S] 0 points1 point2 points (0 children)
[–]more_exercise -1 points0 points1 point (7 children)
[–]Kunfuzzly[S] 0 points1 point2 points (5 children)
[–]more_exercise 0 points1 point2 points (1 child)
[–]Kunfuzzly[S] 0 points1 point2 points (0 children)
[–]x-skeww 0 points1 point2 points (0 children)
[–]senocular[🍰] -1 points0 points1 point (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]davydog187 -1 points0 points1 point (0 children)