-3

which is the difference between "return this" and "return false"

I have this code and I'm trying to understand it

var MicuentaView = function (globalService) {

    var menu;
    var Estatus;

    this.initialize = function() {
        this.$el = $('<div/>');
        this.$el.on('click', '.VeEstatus', this.GoEstatus);
        menu = new MenuView();
    };

     this.render = function(dataToRender) {
        this.$el.html(this.template(dataToRender));
        $('.MENU', this.$el).html(menu.$el);
        console.log('rendereado');
        return this;
    };

    /* events functions */
    this.GoEstatus = function(event){
        event.preventDefault();
        Estatus = new EstatusView();
        $('body', this.$el).html(Estatus.$el);
        return false;   
    };
    this.initialize();
};

Thanks a lot

Pointy
  • 405,095
  • 59
  • 585
  • 614
PolloZen
  • 654
  • 6
  • 12
  • 3
    Well `this` and `false` are (generally) two different things, so returning one is different from returning the other. What exactly is not clear about that? – Pointy Oct 24 '15 at 02:23

2 Answers2

1

Most likely you are calling MicuentaView to create an object as in:

obj = new MicuentaView(something);

"this" will be a reference to the obj that was instantiated. So when you call method "render" as in obj.render(), then obj.render() will return the reference to obj.

So:

result = obj.render();  // assigns the reference of obj to result.

That's useful when you want to chain methods (functions) together, for example

obj.render().toString();

obj.render() returns the obj modified by what render does and then toString() can do its thing on the resulting obj.

"return false;" just returns the boolean value false.

Think of references kind of like pointers in other languages (memory address / location of the object).

ciso
  • 2,887
  • 6
  • 33
  • 58
  • 1
    javascript doesn't have pointers. They are references. see http://stackoverflow.com/questions/17382427/are-there-pointers-in-javascript – clocksmith Oct 24 '15 at 02:39
0

Those are complete different things. false is a boolean, and this is the value of the object which invoked the function (and yes that is an oversimplification).

Griffith
  • 3,229
  • 1
  • 17
  • 30