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

570 questions
1941
votes
17 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
  • 49,340
  • 32
  • 108
  • 136
912
votes
31 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
  • 25,691
  • 19
  • 57
  • 76
584
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
  • 13,045
  • 22
  • 71
  • 107
489
votes
17 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
  • 30,697
  • 19
  • 94
  • 123
251
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
  • 9,835
  • 18
  • 79
  • 134
118
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
  • 56,863
  • 21
  • 114
  • 161
118
votes
3 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…
110
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
  • 2,143
  • 3
  • 17
  • 20
88
votes
6 answers

Can you declare a object literal type that allows unknown properties in typescript?

Essentially I want to ensure that an object argument contains all of the required properties, but can contain any other properties it wants. For example: function foo(bar: { baz: number }) : number { return bar.baz; } foo({ baz: 1, other: 2…
Lucas
  • 14,227
  • 9
  • 74
  • 124
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,668
  • 5
  • 29
  • 28
73
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
  • 12,359
  • 17
  • 65
  • 86
68
votes
10 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
  • 57,621
  • 49
  • 238
  • 373
65
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,998
  • 4
  • 36
  • 59
59
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,112
  • 2
  • 12
  • 19
59
votes
6 answers

How to fill a Javascript object literal with many static key/value pairs efficiently?

The typical way of creating a Javascript object is the following: var map = new Object(); map[myKey1] = myObj1; map[myKey2] = myObj2; I need to create such a map where both keys and values are Strings. I have a large but static set of pairs to add…
Jérôme Verstrynge
  • 57,710
  • 92
  • 283
  • 453
1
2 3
37 38