object-literal

Simple JavaScript question: what is this thing?

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. What is it? And where can I learn more about it? ...

Send Object to remote javascript file

So if i am setting up a yahoo pipes badge on my site, yahoo gives me this code <script src="http://pipes.yahoo.com/js/listbadge.js"&gt;{"pipe_id":"USER_ID","_btype":"list"}&lt;/script&gt; Notice its passing an object literal to the remote script. i would like to do something similar with my own scripts, how do you interact with that...

Object literal + document.body.onclick not doing what I expect

ok, people here is my amazing antigravity library. I'm trying to define it as an object literal, which, uh, i read all the cool kids are doing in order to to encapsulate their libraries. Basic tests indicate it's working properly, but I cannot seem to see document.body from inside my object. what should happen is when I click on the p...

How to Loop through JavaScript object literal with objects as members?

How can I loop through all members in a JavaScript object including values that are objects. For example, how could I loop through this (accessing the "your_name" and "your_message" for each)? var validation_messages = { "key_1": { "your_name": "jimmy", "your_msg": "hello world" }, "key_2": { "your_name": "bi...

Using the object literal notation with jQuery

Hi, the other day I read this article by Alan Storm called "Using jQuery and the Object Literal Singleton/Module Pattern". Now I'm wondering if any of you are writing jQuery code using the object literal notation a lot? I'd be happy to check some code examples and/or get advise on when it actually makes sense to use this notation. Tha...

How can I add a key/value pair to a JavaScript object literal?

Here is my object literal: var obj = {key1: value1, key2: value2}; How can I add {key3: value3} to the object? ...

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 to it's parent, I don't think you can tell this, can you? ...

Javascript: How to create an array of object literals in a loop

Hi, 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 < oFullResponse.results.length; i++) { console.log(oFullResponse.results[i].label); } The value of '...

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*/ ], rows:[ /*here*/ ] } }, relations:{ 0:{ parent:'c...

adding prototype to object literal

STORE = { item : function() { } }; STORE.item.prototype.add = function() { alert('test 123'); }; STORE.item.add(); 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 follow: STORE.item.prototype.add(); ...

JavaScript - Advantages of object literal

I've read that rather than simply writing a bunch of functions, I should use object literal. Can someone explain what the advantages of object literal are with examples, because I don't understand thus far. Thanks ...

in javascript is it possible to construct an object literal with expressions evaluating to strings for property names?

i.e. is it possible to do this: var fruit = "banana"; var x = { "app" + "le" : 5, // "apple" : 5 function(){return "orange"} : 8, // "orange" : 8 "" + fruit : 3 // "banana" : 3 }; ...

Why can't I save an object's methods as properties of another object literal

The code below is used to note some methods to run in particular circumstances so they can be called using a simpler syntax. var callbacks = {alter: SPZ.sequenceEditor.saveAndLoadPuzzle, copy: SPZ.sequenceEditor.saveAsCopyAndLoadPuzzle, justSave:SPZ.sequenceEditor.saveAndLoadPuzzle}; But the code keeps retu...

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<arr.length;i++) { // some literal push method here! /* literal = { one : "", two : "", three : "" } */ } Thus I want the result to be like this: literal =...

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", maxlength: "maxLength", cellspacing: "cellSpacing", rowspan: "rowSpan", colspan: "co...

Passing unknown amounts of variables using through a string string and eval and multiple functions and all sorts

I'm not sure how best to describe this problem... In short, I want to use object literal to allow me to pass a random amount of variables in any order to a function. Whilst this is not big deal in theory, in my code, this object literal is passed to a second function call on_change. on_change works comparing an element inner HTML to a s...

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 in a string literal; is this strictly an interpreter/parsing issue? Is this a candidate f...

Javascript object literal: why can't I do this?

I have the following (simplified) object literal. The icons method uses closure to hide the icons variable, which I'd like to have as an associative array for later lookups. var MapListings = { icons: function () { var allIcons = [] ; return { add: function (iconType, iconImage) { var ico...

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 arguments: class literal(object): def __init__(self, **kwargs): for (k,v) in kwargs.iter...

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 in Python: class Book(Resource): path = "/book/{id}" def get(request): ...