Forum Moderators: open

Message Too Old, No Replies

Please explain these js declarations for me

         

AffiliateDreamer

8:08 pm on Apr 10, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What exactly are the following 2 code snippets doing?

var o = $('#form_area_'+ zid);

---------------

function EatCookies() { };
EatCookies.lasttime = false;

EatCookies.data =
{
aid: null,
bid: null,
sometext: ''
};

Fotiman

9:38 pm on Apr 10, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month




var o = $('#form_area_'+ zid);

That's declaring a variable 'o' and assigning it the value of whatever is returned by the $() method. I believe the Prototype.js library has a $() method which is used to get an element by it's id. For example, if you have in your document something that has:

id="form_area_1"

Then if zid == 1, the above code would return a reference to that node and it would be assigned to o.


function EatCookies() { };

That created a function EatCookes that does nothing. Based on how this is being used, a better method would be to do this, since it doesn't look like it's really being used as a function:

var EatCookies = {};


EatCookies.lasttime = false;

That is assigning a value of false to a property "lasttime" of EatCookies.


EatCookies.data =
{
aid: null,
bid: null,
sometext: ''
};

The {} is used to create "object literals". An object literal contains key/value pairs. The above example creates an object and assigns it to the data property of the EatCookies object. This new object has 3 properties (aid, bid, and sometext). You can access those properties by doing:

EatCookies.data.aid = "foo";
EatCookies.data.bid = "bar";
alert( EatCookies.data.aid + " " + EatCookies.data.bid );

Note, the values of those properties can contain strings, arrays, boolean values, other objects, etc. Which means you could do something like this:

EatCookies.data.aid = [10,25,80]; // an array
EatCookies.data.aid = true; // a boolean
EatCookies.data.aid = {fname:"John",lname:"Smith"}; // an object

Dabrowski

9:43 pm on Apr 10, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



ok here goes:

var o = $.....

I believe there is a function called $ which returns a list of object references based on it's parameters.

It's basically a lot of getElementById statements and returns an array. In this case it will find the element called '#form_area_'+ zid.

function EatCookies() { };

Is a function that does nothing. The next couple of lines set custom properties of the function (lasttime and data), looks like it's being set up for later use.

Dabrowski

9:48 pm on Apr 10, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



My post left at same time as Fotiman, read his it's better!

AffiliateDreamer

11:06 pm on Apr 10, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks allot Fotiman.

I was looking around on the web and it makes more sense now (after your explanation also!).

So I learned that: When we declare a function, javascript actually creates an object in the background that is the same name as the function.

[edited by: AffiliateDreamer at 11:09 pm (utc) on April 10, 2007]