foo vs. foo()

object to behave differently based on index ending SO Question: http://stackoverflow.com/questions/6275795/ Solution provided by Wladimir Palant, and Casey Hope

by majidf

HTML

<div id="no-alert-please">&nbsp;</div>

JavaScript

var baseObject = {

    foo: function() {
        $('#no-alert-please').append('<div>foo is calling you.</div>');
    },

    bar: function() {
        $('#no-alert-please').append('<div>bar is a good place</div>');
    }
};

baseObject.foo.toString = function() {
    return 'If you want to do foo, call this function';
};
baseObject.bar.toString = function() {
    return 'This is yet another method of the base object';
};


$('#no-alert-please').append('<div>' + baseObject.foo + '</div>');
baseObject.foo();

$('#no-alert-please').append('<div>' + baseObject.bar + '</div>');
baseObject.bar();