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
6
votes
1 answer

How is "first-name" a reserved word in JavaScript?

Note: This question pertains to the book 'JavaScript: The Good Parts' written by Doug Crockford. As I was reading a chapter on Objects, I came across a statement as follows: The quotes around a property's name in an object literal are optional if…
BinaryCat
  • 1,220
  • 2
  • 17
  • 32
6
votes
6 answers

How do I slice an array from an array of object literals?

I have this array, in which each index contains an object literal. All of the object literals have the same properties. Some of the object literals have the same value for a given property, and I want to create a new array containing only those…
Ian Campbell
  • 2,678
  • 10
  • 56
  • 104
5
votes
1 answer

Provide one of object types in typescript

If I type the following: interface A { x: number } interface B { y: number } type Z = A | B; // here it allows me to create a variable of type Z with both members of type A and B. let z: Z = { x: 5, y: 6, } I'm not able to ensure…
lort
  • 1,458
  • 12
  • 16
5
votes
1 answer

In Scala.js, how to best create an object conforming to a trait from a JS façade?

Suppose I have this definition in Scala.js for the return type of a function of some library I'm using: @native trait Link extends Object { val href: String = native val title: String = native } What's the best, typesafe way, in Scala code, to…
Jean-Philippe Pellet
  • 59,296
  • 21
  • 173
  • 234
5
votes
6 answers

Appending to a JS object that is not an array?

I have an object that looks something like this { "_id": "DEADBEEF", "_rev": "2-FEEDME", "name": "Jimmy Strawson", "link": "placeholder.txt", "entries": { "Foo": 0 } } Which is read into my javascript with a $.getJSON call. So I…
5
votes
2 answers

TypeScript: Accessing outer "this" from literal getter

When using getters and setters in object literals, I cannot see an easy way to access the outer "this" scope in Typescript. Consider the following: class Report { stuff: any[]; options = { length: 10, get maxLength() {…
Yona Appletree
  • 8,801
  • 6
  • 35
  • 52
5
votes
3 answers

JSON.parse is giving an "undefined" object

I tried to parse this string : [{"ZoneId": "1", "0": "1", "ZoneX": "29", "1": "29", "ZoneY": "27", "2": "27", "ZoneWidth": "76", "3": "76", "ZoneHeight": "61", "4": "61", "ZoneImage": "46", "5": "46", "ZonePointTo": "2", "6": "2"}, {"ZoneId":…
richerlariviere
  • 799
  • 2
  • 13
  • 29
5
votes
5 answers

Extending object literal

var x = { name: "japan", age: 20 } x.prototype.mad = function() { alert("USA"); }; x.mad(); The above code does not work. object literals cannot be extended? or x.mad() not the right way to call.
user3763367
  • 127
  • 2
  • 7
5
votes
2 answers

Getting sibling value of key in a JavaScript object literal

Does anybody know of there's a way to reference the value of a sibling key in a JavaScript object literal? so use the value of target in the beforeNext() function here: obj: { target: 'li.player a.icon-tag', parent: 'ul#drop_list', …
5
votes
2 answers

Javascript object literal. object[i].variable

I have a function where I get some JSON, and then extract the first element, based on some info in an object literal. My problem is: function foo(string){ return data[0].string; } This does not work. What is the correct syntax? The full code is: …
4
votes
1 answer

object literal used in inherited property changes all instances of the class

I have the following code (ObjA) and it works as I would expect, instance 'a1' has the same properties as 'a2' but with different values. function ObjA() { this.objLiteral = {}; this.propA = 0; } var a1 = new ObjA(); a1.objLiteral['hello']…
supercoco
  • 512
  • 2
  • 7
  • 25
4
votes
3 answers

How to use properties of an object literal without being inside a function in javascript

I created an object literal in JS, but wanna to access a property without being in a function. Whenever I try, I don't get any results or it returns null. JS: var settings = { prev: '#prev', next: '#next', slides:…
Shaoz
  • 10,573
  • 26
  • 72
  • 100
4
votes
2 answers

JavaScript Object Creation Pattern

I was reading an article here: http://javascriptweblog.wordpress.com/2010/03/16/five-ways-to-create-objects/ It tells about five ways of creating objects. But my question is one of his way (3) is: myApp.Notepad = function(defaultFont) { var …
Dev555
  • 2,128
  • 4
  • 30
  • 40
4
votes
3 answers

Can a Javascript literal object fire events?

I have a Javascript object literal: var Toolbar = { init: function(toolbar) { this.Bar = $(toolbar); // scope is Toolbar object literal this.Bar.find('clearButton').click(function() { this.trigger('clear'); //…
Daniel T.
  • 37,212
  • 36
  • 139
  • 206
4
votes
5 answers

Sorting a Javascript object by value

In my Javascript application I have an Object and I need to be able to order the array by a value within the Inner Object. For example: { a : { timestamp: xxxxxx other : yyyyyy }, b : { timestamp: xxxxxx …
RobertPitt
  • 56,863
  • 21
  • 114
  • 161