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
19
votes
5 answers

Calling a function in javascript object literal notation declaration

I'm trying to call a function within an object literal that I created, using the this keyword. But an error shows up saying this.doTheMove() is not a function: window.onload = function(){ var animBtn = document.getElementById('startAnim'); …
Shaoz
  • 10,573
  • 26
  • 72
  • 100
17
votes
10 answers

typescript optional property with a getter

This is a simplified example: class PersonParms{ name:string; lastName:string; age?:number; get fullName(){return this.name + " " + this.lastName;} } class Person{ constructor(prms:PersonParms){ } } new…
tru7
  • 6,348
  • 5
  • 35
  • 59
16
votes
4 answers

how to convert a JavaScript literal object into Json object in PHP

I have a JS literal object string such as {name:{first:"George",middle:"William"},surname:"Washington"} and I have to convert it in Json. How can I do it using PHP?
tic
  • 4,009
  • 15
  • 45
  • 86
16
votes
2 answers

Javascript binding using call with setInterval

How can I use "call" with "setInterval" to get an object literal to invoke one of its own methods? Here's an example. This works, and I understand why it works. The timer object calls its own tick method once each second var timer = { start:…
d13
  • 9,817
  • 12
  • 36
  • 44
15
votes
3 answers

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: this.var1 + this.var2 }) obviously this does not…
Hailwood
  • 89,623
  • 107
  • 270
  • 423
15
votes
3 answers

How to add private variable to this Javascript object literal snippet?

Found this at MDC but how if I'd wanted to add a private variable to the var dataset = { tables:{ customers:{ cols:[ /*here*/ ], rows:[ /*here*/ ] }, orders:{ cols:[ /*here*/ ], …
Adam Asham
  • 1,519
  • 2
  • 20
  • 32
14
votes
2 answers

Javascript "abstract method"

My terminology is a bit off, so feel free to correct where necessary. I want to overload a function in javascript and the 'base class' to make use of the overloaded method as well as the inherited class to access the base classes methods. So far, I…
orange
  • 7,755
  • 14
  • 75
  • 139
13
votes
0 answers

How do I properly run the VJET development tools for NodeJS on Eclipse?

The default javascript editor for Eclipse has very poor outlining and code completion. As a result of this, for any modern javascript application like ExtJS or NodeJS where you need to write a lot of object literal statements, Eclipse becomes pretty…
Redsandro
  • 11,060
  • 13
  • 76
  • 106
12
votes
3 answers

How to Sort a JS Object Literal?

If I have this JS object literal: var foo = { Sussy: 4, Billy: 5, Jimmy: 2, Sally: 1 }; How can I create a new, sorted object literal: var bar = { Sally: 1, Jimmy: 2, Sussy: 4, Billy: 5 };
edt
  • 22,010
  • 30
  • 83
  • 118
11
votes
3 answers

why doesn't 'this' of an arrow function change inside an nested object literal?

I found that 'this' keyword seems always point to global in using arrow function inside a nested object literal. According to other questions, the following snippet can be explained as an arrow function's 'this' is defined in lexical context. var c…
11
votes
3 answers

Does Python support something like literal objects?

In Scala I could define an abstract class and implement it with an object: abstrac class Base { def doSomething(x: Int): Int } object MySingletonAndLiteralObject extends Base { override def doSomething(x: Int) = x*x } My concrete example…
deamon
  • 89,107
  • 111
  • 320
  • 448
10
votes
1 answer

Reference nested 'sibling'-property in object literal

I want to reference a nested property in an object literal from within another property in that same object literal. Consider the following contrived example: var obj = { product1: { price: 80, price_was: 100, discount:…
Geert-Jan
  • 18,623
  • 16
  • 75
  • 137
10
votes
4 answers

Is this Javascript object literal key restriction strictly due to parsing?

Please refer to the code below, when I "comment in" either of the commented out lines, it causes the error (in IE) of "':' expected". So then is my conclusion correct, that this inability to provide a reference to an object value, as an object key…
Dexygen
  • 12,287
  • 13
  • 80
  • 147
10
votes
3 answers

Dynamic object literal in javascript?

Is it possible to creat an object literal on the fly? Like this: var arr = [ 'one', 'two', 'three' ]; var literal = {}; for(var i=0;i
marko
  • 10,684
  • 17
  • 71
  • 92
9
votes
5 answers

Convert object literal notation to array

I used a literal as a dictionary, but a third party binding tool only takes arrays. This is one way, is there a better one? var arr = []; $.each(objectLiteral, function () { arr.push(this); });
Anders
  • 17,306
  • 10
  • 76
  • 144
1 2
3
37 38