Questions tagged [object-literal]

JavaScript Object Literal An object literal is a comma separated list of name value pairs wrapped in curly braces. In JavaScript an object literal is defined as follows: var myObject = { sProp: 'some string value', numProp: 2, bProp: false };

Literal means fixed value in source code. In ECMAScript scripting language its possible to represent object as literal. e.g.

  var newobj = {
  var1: true,
  var2: "very interesting",
  method1: function () {
    alert(this.var1)
  },
  method2: function () {
    alert(this.var2)
  }
};
newobj.method1();
newobj.method2(); 

Attribution : http://en.wikipedia.org/wiki/Literal_%28computer_programming%29

Objects in JavaScript

513 questions
1591
votes
26 answers

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 field key3 with value3 to the object?
James Skidmore
  • 44,302
  • 30
  • 104
  • 135
788
votes
25 answers

Self-references in object literals / initializers

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 throws a reference error since this doesn't refer to…
kpozin
  • 21,813
  • 17
  • 52
  • 71
489
votes
16 answers

How to use a variable for a key in a JavaScript object literal?

Why does the following work? .stop().animate( { 'top' : 10 }, 10 ); Whereas this doesn't work: var thetop = 'top'; .stop().animate( { thetop : 10 }, 10 ); To make it even clearer: At the moment I'm not able to pass a…
speendo
  • 11,483
  • 19
  • 66
  • 100
436
votes
15 answers

JavaScript property access: dot notation vs. brackets?

Other than the obvious fact that the first form could use a variable and not just a string literal, is there any reason to use one over the other, and if so under which cases? In code: // Given: var foo = {'bar': 'baz'}; // Then var x =…
Mark Renouf
  • 29,573
  • 19
  • 89
  • 120
233
votes
9 answers

How to create an array of object literals in a loop?

I need to create an array of object literals like this: var myColumnDefs = [ {key:"label", sortable:true, resizeable:true}, {key:"notes", sortable:true,resizeable:true},...... In a loop like this: for (var i = 0; i <…
codecowboy
  • 8,963
  • 17
  • 73
  • 129
100
votes
8 answers

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.applicationPath : "Application " +…
RobertPitt
  • 54,473
  • 20
  • 110
  • 156
94
votes
4 answers

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 =…
balafi
  • 1,905
  • 2
  • 14
  • 20
85
votes
2 answers

Template String As Object Property Name

Why does JavaScript not allow a template string as an object property key? For example, when I input: foo = {`bar`: 'baz'} into the NodeJS REPL, it throws a SyntaxError with "Unexpected template string" with a long stack trace. Property values are…
82
votes
8 answers

Adding/removing items from a JavaScript object with jQuery

I have a JavaScript 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: "action"}, {id: "4", name: "Ordinary…
Bug Magnet
  • 2,608
  • 4
  • 26
  • 28
70
votes
8 answers

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 like this var name; var value; var temp =…
Mike
  • 11,085
  • 15
  • 55
  • 82
64
votes
9 answers

PHP object literal

In PHP, I can specify array literals quite easily: array( array("name" => "John", "hobby" => "hiking"), array("name" => "Jane", "hobby" => "dancing"), ... ) But what if I want array of objects? How can I specify object literal in…
Tomas
  • 52,167
  • 46
  • 207
  • 345
61
votes
5 answers

Javascript 'colon' for labeling anonymous functions?

What does this code refer too? queryString: function() { //some code } I tested it in the WebConsole (Firefox) but it wouldn't execute, so I'm thinking that it isn't equivalent to function queryString() {}. So what is it exactly?
knownasilya
  • 5,421
  • 4
  • 32
  • 58
57
votes
5 answers

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 doesn't this work? However, it works when I use the…
John
  • 1,092
  • 2
  • 11
  • 19
50
votes
8 answers

Are object literals Pythonic?

JavaScript has object literals, e.g. var p = { name: "John Smith", age: 23 } and .NET has anonymous types, e.g. var p = new { Name = "John Smith", Age = 23}; // C# Something similar can be emulated in Python by (ab)using named…
ShinNoNoir
  • 2,254
  • 1
  • 17
  • 24
46
votes
6 answers

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 ?
thinkanotherone
  • 2,539
  • 9
  • 25
  • 36
1
2 3
34 35