How to use anonymous objects

I don’t know how to call it and I don’t even know if it has a name. I thought a moment about calling it Mistigri but I already have a cat with that name. I decided to call it anonymous object.

How?

[source:javascript]

var ao = {
name : “Anonymous Object”,
possibleName : “Mistigri”,
presentYourself : function() {
alert(“Hi! My name is ” + this.name + ” but they thought of calling me ”
+ this.possibleName + “.”);
}

ao.presentYourself();

[/source]

By running the code below, you should see a message box with Hi! My name is Anonymous Object but they thought of calling me Mistigri. Isn’t it wonderful?

What can I do with that?

For the moment, I haven’t found a lot of uses for anonymous objects. If you know more, leave a comment and I will add yours to mine. Maybe we’ll end up with two and a half diffrent uses.

Still, I am using it to create enumerations (enum).
[source:javascript]
var DisplayType = {
big : 0,
medium : 1,
small : 2
}

var display = DisplayType.big;
alert(display);
[/source]

This is very useful instead of creating “constants” with different name.

[source:javascript]

var DISPLAY_TYPE_BIG = 0;
var DISPLAY_TYPE_MEDIUM = 1;
var DISPLAY_TYPE_SMALL = 2;

[/source]

Second use

Nicolás Sanguinetti pointed to me another use of anonymous objects.

Saying you have a lot of possible parameters to pass to a method. Instead of having them all list, you use an anonymous object as parameters and the programmer decides which one he will use.

Instead of

[source:javascript]
function doSomething(id, name, label, age, fatherName, brotherName, motherName, grandmaName, isFunny, isShy)
{
var concat = id + ” ” + label + ” ” + isFunny;

// More code…
}

doSomething(2, null, “name”, null, null, null, null, null, true, null);
[/source]

you have

[source:javascript]
function doSomething(params)
{
var concat = params.id + ” ” + params.label + ” ” + params.isFunny;

// More code…
}

doSomething({id : 2, label : “name”, isFunny: true});
[/source]

  • http://nicolassanguinetti.info Nicolás Sanguinetti

    Well, configuration options for methods. The Prototype and Script.aculo.us JS frameworks use this extensively.

    For example, in script.aculo.us, when you want something to fade from one color to another, you do something like Effect.Fade(“my_dom_id”, { from: “#000000″, to: “#ff0000″ });

    (I don’t remember the exact syntax, but the idea is there :) )

  • Aaron Faanes

    These guys are actually called object literals ( http://en.wikipedia.org/wiki/Object_literal ).

  • Joe

    Anonymous objects are Often used to initialize complex arrays.

    Take this example from the ext library for example:

    /*
    * Ext JS Library 2.2
    * Copyright(c) 2006-2008, Ext JS, LLC.
    * licensing@extjs.com
    *
    * http://extjs.com/license
    */

    Ext.onReady(function() {

    var patients = [{
    insuranceCode: '11111',
    name: 'Fred Bloggs',
    address: 'Main Street',
    telephone: '555 1234 123'
    }, {
    insuranceCode: '22222',
    name: 'Fred West',
    address: 'Cromwell Street',
    telephone: '616 555 222'
    }, {
    insuranceCode: '33333',
    name: 'Fred Mercury',
    address: 'Over The Rainbow',
    telephone: '555 321 0987'
    }, {
    insuranceCode: '44444',
    name: 'Fred Forsyth',
    address: 'Blimp Street',
    telephone: '555 111 2222'
    }, {
    insuranceCode: '55555',
    name: 'Fred Douglass',
    address: 'Talbot County, Maryland',
    telephone: 'N/A'
    }];

  • http://0x1ff.wordpress.com/2010/04/30/blog-about-gwt-and-the-future-of-web-development/ Blog about GWT and the Future of Web Development « the joy of coding

    [...] class functions, closures, extension methods, a vastly different “this” context, anonymous objects, dynamic typing and so on. … My position can be summed up as: GWT treats Javascript as a bug [...]

  • this

    hello

    i suppose it would rather be called an instance of an anonymous class, as an anonymous object i think is already used as an expression for the parameter passed (not in the signature but in the calling routine) in the following example :

    Function anyFunction(AnyClass I_oAnyObject) { }

    {
    [...]
    anyFunction(new AnyClass());
    [...]

    }

  • Rue Leonheart

    Anonymous objects are also useful in jQuery.

  • http://kalzggdjsdcvi.com/ dYcxcgcvfgg

    2011…

    I was recommended this blog by my cousin. I am not sure whether this post is written by him as nobody else know such detailed about my problem. You are amazing! Thanks!…

  • http://esoftwarereview.info software reviews

    2011…

    Wonderful beat ! I wish to apprentice while you amend your site, how can i subscribe for a blog website? The account helped me a acceptable deal. I had been tiny bit acquainted of this your broadcast provided bright clear idea…

  • http://www.sportspicksnation.com picks for college football

    2011…

    I have not checked in here for a while as I thought it was getting boring, but the last several posts are good quality so I guess I will add you back to my everyday bloglist. You deserve it my friend :)