在javaScript中返回这个|返回false

Return this | return false in javaScript

本文关键字:返回 false javaScript      更新时间:2023-09-26

这是"return this"answers"return false"的区别

我有这段代码,我想理解它

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 to lot

很可能你正在调用MicuentaView来创建一个对象,如:

obj = new MicuentaView(something);

"this"将是对被实例化的对象的引用。所以当你在obj.render()中调用方法"render"时,obj.render()将返回对obj的引用。

:

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

当你想把方法(函数)链接在一起时,这很有用,例如

obj.render().toString();

obj.render()返回经过render操作修改的对象,然后toString()可以对生成的obj执行操作。

"return false;"只返回布尔值false。

把引用想象成其他语言中的指针(对象的内存地址/位置)。

这完全是两码事。false是一个布尔值,this是调用该函数的对象的值(是的,这是一个过度简化)。