Forum Moderators: open

Message Too Old, No Replies

What the heck is this kind of js?

         

johnhamman

5:49 pm on Apr 12, 2006 (gmt 0)

10+ Year Member



Ok, I am not an expert and hopefully I won't get burned for this question, But I am confussed by the following sample since I don't know where to find tutorials about these methods

var SomeThing = { SomeItem:"String" ,SomeFunction:function { return window.Somthingelse;},initialize:function{startsome code;}} 
SomeThing.initialize();

So my question is I have never seen SomeFunction:function {} with the ":". What's that about and where can I find out more about it. It seems alot of advanced JS code out there uses this method.

DrDoc

6:42 pm on Apr 12, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That's a way of creating an object.

<script type="text/javascript">
foo = { bar:"Hello!", baz:new function() { now = new Date(); return now; } }
alert(foo.bar);
alert("The current date and time is " + foo.baz);
</script>

Little_G

6:47 pm on Apr 12, 2006 (gmt 0)

10+ Year Member



Hi,

johnhamman, try searching for 'object literal syntax' this should give you more info.

Andrew

johnhamman

6:49 pm on Apr 12, 2006 (gmt 0)

10+ Year Member



Thanks Little_G thats exactly what I was looking for.
john

DrDoc

6:54 pm on Apr 12, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



[developer.mozilla.org...]


foo = {};
foo["bar"] = "Hello!";
foo["baz"] = new function() { now = new Date(); return now; };


foo = { bar:"Hello!", baz:new function() { now = new Date(); return now; } }


function createObj() {
this.bar = "Hello!";
this.baz = new function() { now = new Date(); return now; }
}
foo = new createObj();


foo = new Object();
foo.bar = "Hello!";
foo.baz = new function() { now = new Date(); return now; }