I want to use a javascript object dynamically. Examples: {name: "dev.pus", year: 2012, OS: "linux"}
This is not something new. Now you can use the "example" properties with the general category:
console.log (example.name); // or console.log (example.year); But what if I want to take the feature dynamically? For example, another version (assuming that the user sets it) should decide what asset we want:
var = "name"; Console.log (example.var); // error console.log (example [var]); // error What is the way to go?
Your example should work if you change your variable name (var is reserved).
var key = 'name'; Console.log (example [key]); You can repeat all objects on your object again:
for (for example var items) {if ( Example.hasOwnProperty (item)) {Console.log (example [items]); }}
Comments
Post a Comment