Basically i'm having trouble with something that i'm sure is super duper simple but I just don't know what to call it and therefore I am having trouble searching for an answer. :(
Basically, say I've declared an object, i.e var meow = {}; and then i decide to create an object within that by doing something like meow.cutekitten = {}; this is all straight forward and i end up with an object called cutekitten within that.
My issue is, what if I've declared cutekitten as a variable and i want to use the contents of that variable as the name of this new object rather than the variable name?
var propertyName = "cutekitten";
var meow = {};
meow[ propertyName ] = {};
Related
I have part of my code which creates several instances of a Player class by the following code:
player.push(new Player(i, positions[randomPosPlayer]));
Hence I'm not using the conventional way i.e. var somePlayer = new Player(x,y);
Assuming 3 players are created, all of them appear as anonymous Objects. How can I name them while maintaining the key-value of each Object?
I don't know if you are using custructor functions or the es6 class syntax but either way you can "name" your players in it's constructor by something like this:
let allPlayers = {};
function Player(x,y,name){
if(name)allPlayers[name] = this;
//...other constructor stuff
}
and just pass the name when you create it:
player.push(new Player(i, positions[randomPosPlayer],"CustomName"));
and then you can simply get the players by name using
let p = allPlayers["CustomName"];
or even by accessing the attribute directly if you are sure a player with this name exists:
let p = allPlayers.CustomName;
I want to create a new variable every function call, and push this new variable to a global array.
globalArr = [];
function addObj(objId){
var newObj = {id: objId};
globalArr.push(newObj);
}
Javascript's Array.prototype.push method will append a reference to newObj to globalArr.
Will I be creating unique objects for every call to addObj()? I think but am not completely sure that newObj will not persist across multiple calls. I want to make sure I am not pushing references to the same newObj every time I call addObj.
I have considered a solution like the following, but am not sure if it is necessary:
function addObj(objId){
var newObj = new anObject(objId);
globalArr.push(newObj);
}
function anObject(objId){
this.id = objId;
}
Perhaps the problem I speak of doesn't even exist?
var newObj = {id: objId};
This line will guarantee you that its a new object pointing to a different memory address every time. The open and close curly brackets initialize a new object. So this part actually:
var newObj = {};
So yes, pushing that value into a global array every time the function is called will give you X number of new objects where X is the number of times you called the function. It will not be reference to the same object. As you said, that second example of code is not necessary to achieve what you want.
To answer the title of this question a variable declared in a function does not persist over multiple function calls. Its a locally scoped variable that is instantiated anew every time the function is called.
I am new to Javascript and now studying it...
var person = function() {
this.name = "name"
};
var person2 = function() {
var obj = {};
obj.name = "name";
return obj;
};
Let's suppose we have two functions shown above. It seems that objects can be created by using either of the functions. For example)
var p = new person();
var p2 = new person2();
My question is: What's difference between person vs person2? Are they exactly the same? If not which one is a more preferable way to use?
Thanks
The normal way of creating an object is the first way.
The second way will create two objects, and one will be discarded. One object will be created before the function is called, just as with the first method, but because the function returns another object the first object will be discarded and the returned object will be used instead.
An important difference between the methods is that the second one can't use a prototype. Anything that you put in the prototype of the function will be applied to the object that ends up being discarded.
The difference is in the way you use the functions.
The first one is intended to be used as a constructor, this being set to the newly created object. It is intended to be used in combination with the operator new, as follows:
var bill = new person();
This is just like a normal constructor as in typical OOP language.
The second one is intended to be used as a normal function (without new), e.g.:
var bill = person();
You can use this way of object creation in combination with the builder pattern.
Is it possible to create a new Array, giving it the name of the content of a variable?
For example something like this:
var nameofarray = "array_name";
var ¿nameofarray? = new Array();
So that ¿nameofarray? gets the value of "array_name"?
Assuming you are in global scope (called window) so:
var nameofarray = "array_name";
window[nameofarray] = new Array();
Otherwise it's only posible on objects:
function a() {
var nameofarray = "array_name";
var obj = {};
obj[nameofarray] = new Array();
}
You can also use eval. Which is used for evaluating a string to JavaScript code:
eval('var '+ nameofarray+'=new Array()');
This will work in local scopes as well, but I hardly ever recorment it to anyone.
You would maybe like to read: http://javascriptweblog.wordpress.com/2010/04/19/how-evil-is-eval/
In PHP and other languages, they are called variable variables. This might help: Javascript "Variable Variables": how to assign variable based on another variable?
If nameofarray is declared in the global scope, you can use the window object:
window[nameofarray] = []; // Use an array literal instead also
^^^^^^^^^^^^^^^^^^^
But you really shouldn't be doing this.
I'm playing around with bind, and the following works:
webSQL.InsertTransaction = function(qry,CurrentRow) {
var local = {};
// Clone the webSQL.Insert function and add 2 parameters:
local.InsertTransaction = webSQL.Insert.bind(this,qry,CurrentRow);
// Call webSQL.Insert(qry,CurrentRow,Transaction)
dbo.transaction(local.InsertTransaction);
}
webSQL.Insert = function(qry,CurrentRow,Transaction) {}
I'd like to simplify it even more. Can I somehow not have to specify the 2 variables that are in the arguments scope, but instead do something like:
local.InsertTransaction = webSQL.Insert.bind(webSQL.InsertTransaction)
maybe. My thinking is that then webSQL.Insert can reference qry and CurrentRow from it's "this.arguments" thingy.
I'm not sure why you were using the object assigned to the local variable in the first place.
All you were doing was giving it a function, and then taking that function right back out. Why not skip that step?
webSQL.InsertTransaction = function(qry,CurrentRow) {
dbo.transaction(webSQL.Insert.bind(this,qry,CurrentRow));
}