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
9
votes
6 answers

How can I invoke a member function using bracket notation?

var objectliteral = { func1:fn(){}, func2:fn(){}, ................. funcn:fn(){} } I know I can invoke methods from that object literal using dot notation this: objectliteral.func1(); But I would like to do that using array…
Stann
  • 13,518
  • 19
  • 65
  • 73
9
votes
8 answers

What is this thing in JavaScript?

Consider: var something = { wtf: null, omg: null }; My JavaScript knowledge is still horribly patchy since I last programmed with it, but I think I've relearned most of it now. Except for this. I don't recall ever seeing this before. …
Daddy Warbox
  • 4,500
  • 9
  • 41
  • 56
9
votes
4 answers

Can't define variable in JavaScript object literal

Why does this code work... var message = { texts: { text1: 'Hello', text2: 'World' }, greet: function() { console.log(this.texts.text1 + ' ' + this.texts.text2 + '!'); } } message.greet(); ...but this…
Robert Kirsz
  • 1,257
  • 1
  • 13
  • 15
9
votes
3 answers

How to extend object-literal objects for events in NodeJS?

I am trying to have my Node.js object cast events. Now this is not a problem if I make 'static' objects and instantiate them, but how do I do this when my object has no static grandpa, like when an object was created using object literal notation? I…
Redsandro
  • 11,060
  • 13
  • 76
  • 106
8
votes
3 answers

JavaScript Object literal method: Recursive call

Is it possible to call recursively a method from an object literal? For example: (function () { 'use strict'; var abc = ['A', 'B', 'C'], obj = { f: function () { if (abc.length) { …
8
votes
3 answers

Typescript object literal "this" keyword

What's the expected behavior when using this inside a function in an object literal? For example, let's say I have a type foo that only has a function named bar and no other property on it. But in the fooObj.bar method, I'm able to access this.baz…
8
votes
3 answers

Javascript object literal: value initialization?

i was using object literal to create an object with methods. Here a simple example. var SizeManager = { width : 800, height : 600, ratio : this.width / this.height, resize : function (newWidth) { width = newWidth; …
bonfo
  • 2,110
  • 3
  • 17
  • 14
8
votes
2 answers

"SyntaxError: Unexpected token :" when inputting { "a": "", "b": "" } json in console

I'm getting errors, in both chrome and firefox developer tools, when trying to evaluate the following: { "a": "", "b": "" } jsonlint.com tells me it's valid. Putting this code in an actual javascript file and running it works fine. The…
morgancodes
  • 25,055
  • 38
  • 135
  • 187
7
votes
4 answers

How to access an outer member from a nested object literal?

In the following code, can the x member be accessed from the nested object literal? var outer = { x : 0, inner: { a : x + 1, // 'x' is undefined. b : outer.x + 1, // 'outer' is undefined. c : this.x + 1 //…
Nixuz
  • 3,439
  • 4
  • 39
  • 44
7
votes
1 answer

typescript initialize object with partial object literal

I want to initialize an instance of a class with an object literal not containing all the elements in the class but whose elements are in the class. class ATest{ aStr:string; aNum:number; result(){return this.aStr + this.aNum;} } let…
tru7
  • 6,348
  • 5
  • 35
  • 59
7
votes
2 answers

Puttiing comments in javascript object literals

Is it possible to put comments within javascript object literals? The example below works for me in Firefox, but I cannot find any clear documentation on this. Also, all the examples I look at seem to avoid comments in object literals. var o = { …
andco42
  • 123
  • 2
  • 4
7
votes
3 answers

To count the number of objects in object literal using Jquery

Code: var animals = { "elephant": { "name" : "Bingo", "age" : "4" }, "Lion": { …
Earth
  • 3,477
  • 6
  • 37
  • 78
6
votes
3 answers

Avoid rerender in React caused by objects literal : how to do with variables in the object?

I read in this article React is Slow, React is Fast: Optimizing React Apps in Practice that : In fact, each time you pass an object literal as prop to a child component, you break purity. Alright, I got it. So the best to avoid that is to create a…
arnaudambro
  • 2,403
  • 3
  • 25
  • 51
6
votes
1 answer

Three.js "Uncaught TypeError: Cannot read property 'render' of undefined" error with object literal

I'm trying to convert my codes to object literal style. I can create the scene but I got problems with animation. In this line I'm getting the "Uncaught TypeError: Cannot read property 'render' of undefined" error. this.renderer.render(this.scene,…
ozgrozer
  • 1,824
  • 1
  • 23
  • 35
6
votes
3 answers

Why are curly brackets needed when using jQuery to adjust width and height?

The following code to double an objects width and height works fine. I just can't understand why curly brackets are needed. var target = $('#target'); target.css({ width: target.width() * 2, height: target.height() * 2 });
Alvin
  • 81
  • 3