How to initialize a javascript object

satya - Saturday, August 08, 2009 11:19:50 AM

Basics


//Initialize an empty object
var someobj = {};

//then you can do
someobj.field1=10;
someobj.field2="string";

//Or you can do to begin with
var someobj = {field1:10,field2:"string");

//You can use this an array of objects as well
var someobjArray = [
  {field1:10,field2:"string");
  {field1:10,field2:"string");
  {field1:10,field2:"string");
];

//you can have nested objects as well
var someobj = {field1:10,
   field2:"string", 
   field3:{field1:10,field2:"string"));

satya - Saturday, August 08, 2009 11:23:07 AM

How can I initialize a javascript object from a string representation

How can I initialize a javascript object from a string representation

Search for: How can I initialize a javascript object from a string representation

satya - Saturday, August 08, 2009 11:30:14 AM

Let's take a look at the javascript ref page at mozilla

Let's take a look at the javascript ref page at mozilla

satya - Saturday, August 08, 2009 11:36:24 AM

javascript extra parentheses syntax literal notation

javascript extra parentheses syntax literal notation

Search for: javascript extra parentheses syntax literal notation

satya - Saturday, August 08, 2009 11:43:42 AM

Leading to a discussion on JSON

Leading to a discussion on JSON

satya - Saturday, August 08, 2009 12:08:25 PM

use of ( ) parantheses in javascript

use of ( ) parantheses in javascript

Search for: use of ( ) parantheses in javascript

satya - Saturday, August 08, 2009 12:14:44 PM

Some explanation pointing to closures and lifetimes

Some explanation pointing to closures and lifetimes

satya - Saturday, August 08, 2009 12:16:09 PM

The claim is...

(function())() implies define an annonymous function and call that function immediately.

It is not clear how this will expand the scope of variables etc.

satya - Saturday, August 08, 2009 12:24:34 PM

Another good discussion, again, leading to closures...

Another good discussion, again, leading to closures...

satya - Saturday, August 08, 2009 12:31:06 PM

Pretty good discussion on the topic of parens

Pretty good discussion on the topic of parens

satya - Saturday, August 08, 2009 12:46:55 PM

See if some functional programming detour helps

See if some functional programming detour helps

satya - Saturday, August 08, 2009 12:51:32 PM

Here is an interesting code from that


( function () 
{     
   var s=document.createElement("script");         
   s.src="http://mydomain.com/script.js";     
   document.body.appendChild(s);  
})();

satya - Saturday, August 08, 2009 1:07:03 PM

json enclosing parens parentheses

json enclosing parens parentheses

Search for: json enclosing parens parentheses

satya - Saturday, August 08, 2009 1:14:11 PM

An interesting read

An interesting read

The gist of it is

eval accepts a sequence of Javascript statements. The Javascript parser interprets the ?{? token, occuring within a statement as the start of a block and not the start of an object literal.

When you enclose your literal into parentheses like this: ({ data_from_the_wire }) you are switching the Javascript parser into expression parsing mode. The token ?{? inside an expression means the start of an object literal declaration and not a block, and thus Javascript accepts it as an object literal.

satya - Saturday, August 08, 2009 1:17:41 PM

javascript parsing statement mode expression mode

javascript parsing statement mode expression mode

Search for: javascript parsing statement mode expression mode

satya - Saturday, August 08, 2009 1:29:24 PM

An article on closures

An article on closures

satya - Saturday, August 08, 2009 1:39:20 PM

The above article has a good explanation of namespacing with closures..

that explain how the returned set of functions from executing a function object can continue to use variables that are denfined in that non existing function after the call.