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
53
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,274
  • 1
  • 18
  • 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,905
  • 9
  • 29
  • 37
45
votes
3 answers

Call functions from function inside an object (object literal)

I'm learning to use object literals in JS, and I'm trying to get a function inside an object to run by calling it through another function in the same object. Why isn't the function "run" running when calling it from the function "init"? var RunApp…
holyredbeard
  • 19,619
  • 32
  • 105
  • 171
44
votes
5 answers

What is the call signature of an object literal type and how can they be used with generic types?

I'm reading this section of the TypeScript documentation, under the generic types section, the following two are stated to be the equivalent: Code Sample 1 function identity(arg: T): T { return arg; } let myIdentity: (arg: T) => T =…
tomhughes
  • 4,597
  • 2
  • 24
  • 33
40
votes
5 answers

JavaScript object literal length === undefined?

I am working on this animation function but I have a problem. I can't seem to perform what should be an easy task, I can not get the length of an object. If you check out that jsFiddle you can see that I am running alert(properties.length); and it…
Olical
  • 39,703
  • 12
  • 54
  • 77
38
votes
1 answer

Object literal vs constructor+prototype

Object literal=name value pairs wrapped in curly braces. Constructor=a function used to create multiple instance using keyword new. Prototype=for extension of a literal. This is what I have understood till now.But the more I research,More I get…
HIRA THAKUR
  • 17,189
  • 14
  • 56
  • 87
38
votes
4 answers

JavaScript - Advantages of object literals

I've read that rather than simply writing a bunch of functions, I should use an object literal. What are the advantages of object literal, with examples?
Jourkey
  • 33,710
  • 23
  • 62
  • 78
36
votes
6 answers

How can I differentiate an object literal from other Javascript objects?

Update: I'm rephrasing this question, because the important point to me is identifying the object literal: How can I tell the difference between an object literal and any other Javascript object (e.g. a DOM node, a Date object, etc.)? How can I…
Will McCutchen
  • 13,047
  • 3
  • 44
  • 43
36
votes
1 answer

Arrow Function in Object Literal

I'm trying to figure out why an arrow function in an object literal is called with window as this. Can someone give me some insight? var arrowObject = { name: 'arrowObject', printName: () => { console.log(this); } }; // Prints: Window…
33
votes
7 answers

Can one set multiple properties inside an object literal to the same value?

For example, can I do this?: { a: b: c: d: 1, e: 2, geh: function() { alert("Hi!") } } EDIT: Is there some way I can avoid doing this?: { a: 1, b: 1, c: 1, d: 1, e: 2, geh: function() { alert("Hi!") } }
PitaJ
  • 12,969
  • 6
  • 36
  • 55
30
votes
5 answers

Why are some object-literal properties quoted and others not?

I see this all the time: object literals declared such that some keys are surrounded with quotes and others are not. An example from jQuery 1.4.2: jQuery.props = { "for": "htmlFor", "class": "className", readonly: "readOnly", …
ntownsend
  • 7,462
  • 9
  • 38
  • 35
28
votes
1 answer

Why is the dict literal syntax preferred over the dict constructor?

Why is the Python dict constructor slower than the using literal syntax? After hot debate with my colleague, I did some comparison and got the following statistics: python2.7 -m timeit "d = dict(x=1, y=2, z=3)" 1000000 loops, best of 3: 0.47 usec…
Fabz
  • 303
  • 1
  • 3
  • 5
27
votes
10 answers

How to determine if an object is an object literal in Javascript?

Is there any way to determine in Javascript if an object was created using object-literal notation or using a constructor method? It seems to me that you just access it's parent object, but if the object you are passing in doesn't have a reference…
leeand00
  • 25,510
  • 39
  • 140
  • 297
23
votes
3 answers

JavaScript data structure for fast lookup and ordered looping

Is there a data structure or a pattern in JavaScript that can be used for both fast lookup (by key, as with associative arrays) and for ordered looping? Right, now I am using object literals to store my data, but I just discovered that Chrome does…
Haes
  • 12,891
  • 11
  • 46
  • 50
20
votes
4 answers

Safely parsing a JSON string with unquoted keys

json2.js is strict requiring all object keys be double-quoted. However, in Javascript syntax {"foo":"bar"} is equivalent to {foo:"bar"}. I have a textarea that accepts JSON input from the user and would like to "ease" the restriction on double…
daepark
  • 256
  • 1
  • 2
  • 5
1
2
3
37 38