Using a string to create a new object


#1

I was wondering if it is possible to use contents in a string to create a new object.

For example:

var name = "John";

now I would like to create an object called John. Instead of writting:

var John = new Object();

is it possible to use the contents in 'name' to make that object?


#2

The right hand side is a string primitive. The left hand side is a property of the context object, which in this case is window. In other words, a global variable called name.

One problem though, name is a reserved word in the global namespace. It needs to be scoped to a new object context..

var john = new Object();
john.name = "John";

Or we could write the literal,

var john = {
    name: "John"
};

We can also write it in a function (which is an object)...

function Person(name) {
    this.name = name;
}
john = new Person("John");

Or just inside a function...

function getName() {
    name = prompt("What is your name?");
    console.log("Hello, " + name + ".");
    return name;
}
john = getName();

In any of these contexts, the variable name is legitimate. Just not in window. The name property is intended to identify the actual window, not some other arbitrary value in our program. Best to just leave it be in global scope.


#3

I'm sorry, I'm still new to programming and I am not able to express my question accurately.
maybe if I tried another example to explain my problem more clearly =x

lets say I have an array

var list = ["john","mary","kate"];

now I would like to write a function that will create new variables called 'john', 'mary' and 'kate' using the array 'list'

how would I do that?


#4

var list = ["john","mary","kate"];
for (var i = 0; i < list.length; i++) {
    window[list[i]] = null;
}
console.log(john, mary, kate);
// null null null

It is vitally important that none of the strings in list coincide with JS reserved words. This will lead to conflicts. It also needs to be considered whether other variables may exist with the same name. They will be overwritten by this.


#5

Yes, this is what I was looking for! Thank you! :grin:


#6

Now that we have established that we can create new window variables, we can make them anything we want.

    window[list[i]] = {};

Now we can populate each object as we choose.

john.name = "John";
mary.name = "Mary";
kate.name = "Kate";

Eventually one finds this approach rather tedious and inefficient, but it still bears learning the basic mechanics. Definitely bears learning.


#7

var list = ["john","mary","kate"];
for (var i = 0; i < list.length; i++) {
    window[list[i]] = {};
    window[list[i]].name = list[i][0].toUpperCase() + list[i].substring(1);
}
console.log(john.name, mary.name, kate.name);
// John Mary Kate

#8

Haha, it seems now you know exactly what I'm trying to do. All this is very helpful. Thank you! :grin:


#9

Off topic rhetoric:

It always takes more time and effort than we expect; so long as we keep forging on, right? Understanding is hard. In family, friends, school, business, and programming it boils down to the same thing... We cannot be shy of work.

You see what we went through together to get where we are? It's always going to follow a similar pattern as you move on to bigger challenges. Keep this progressive approach in mind and expect failure along the way. All part of the learning process, rest assured. Strengthen your understanding by the failures. Successes emerge from this.


#10

you have beautiful handwriting.