All Questions

414
votes
17answers
81774 views

Self-references in object literal declarations

Is there any way to get something like the following to work in JavaScript? var foo = { a: 5, b: 6, c: this.a + this.b // Doesn't work }; In the current form, this code obviously throw...
219
votes
6answers
76608 views

Using a variable for a key in a JavaScript object literal

Why does the following work? <something>.stop().animate( { 'top' : 10 }, 10 ); Whereas this doesn't work: var thetop = 'top'; <something>.stop().animate( { thetop : 10 }, 10 );...
867
votes
20answers
1217243 views

How can I add a key/value pair to a JavaScript object?

Here is my object literal: var obj = {key1: value1, key2: value2}; How can I add {key3: value3} to the object?
76
votes
6answers
383311 views

Adding/removing items from JSON data with JQuery

I have a JSON object as follows: var data = {items: [ {id: "1", name: "Snatch", type: "crime"}, {id: "2", name: "Witches of Eastwick", type: "comedy"}, {id: "3", name: "X-Men", type: "act...
55
votes
8answers
23158 views

dynamic keys for object literals in Javascript

Ok so I'm working away on a project in Nodes, and I've come across a small problem with the keys in object literals, I have the following set-up: var required = { directories : { this.app...
46
votes
4answers
19904 views

Adding Prototype to JavaScript Object Literal

STORE = { item : function() { } }; STORE.item.prototype.add = function() { alert('test 123'); }; STORE.item.add(); I have been trying to figure out what's wrong with this quite a while. Why doe...
36
votes
6answers
36007 views

Use a concatenated (dynamic) string as JavaScript object key?

var test = "test123" var test123 ={ "key" + test: 123 } This code doesn't work. What is wrong with "key" + test ?
54
votes
4answers
55614 views

create object using variables for property name

Is it at all possible to use variable names in object literal properties for object creation? Example function createJSON (propertyName){ return { propertyName : "Value"}; } var myObject = crea...
44
votes
6answers
159429 views

Dynamically Add Variable Name Value Pairs to JSON Object

I have a json object full of ips like var ips = {} I then add ip objects to this object like so ips[ipID] = {} I then need to add dynamic/variable name value pairs to each ip so I am using code ...
12
votes
4answers
7729 views

reference variable in object literal?

say I have myfunc({ var1: 1, var2: 2, }) if i want to have a value that makes use of the current unnamed object, how would I do this? eg if I wanted myfunc({ var1: 1, var2: 2, var3: thi...

Previous Next