-1

I have a function I can access like : this.randomNumber() which output a random number like : 54851247

The goal of the question is to access this function into another function.

console.log(this.randomNumber()); // Output: 54851247
function anotherFunction() {
    console.log(this.randomNumber()); // Error
}

Error I get :

Uncaught TypeError: this.createTorrentId is not a function

Full code :

Ext.ns("Deluge.add");
Deluge.add.FileWindow = Ext.extend(Deluge.add.Window, {
    title: _("Add from File"),
    layout: "fit",
    width: 350,
    height: 115,
    modal: !0,
    plain: !0,
    buttonAlign: "center",
    closeAction: "hide",
    bodyStyle: "padding: 10px 5px;",
    iconCls: "x-deluge-add-file",
    initComponent: function() {
        Deluge.add.FileWindow.superclass.initComponent.call(this);
        this.addButton(_("Add"), this.onAddClick, this);
        this.form = this.add({
            xtype: "form",
            baseCls: "x-plain",
            labelWidth: 35,
            autoHeight: !0,
            fileUpload: !0,
            items: [{
                xtype: "fileuploadfield",
                name: "file",
                buttonCfg: {
                    text: _("Browse") + "..."
                }
            }]
        })
    },
    onAddClick: function(c, b) {
        console.log(this.randomNumber()); // Output: 54851247
        function anotherFunction() {
            console.log(this.randomNumber()); // Error
        }
    },
})

Declaration of the function:

Ext.ns("Deluge.add");
Deluge.add.Window = Ext.extend(Ext.Window, {
    initComponent: function() {
        Deluge.add.Window.superclass.initComponent.call(this);
        this.addEvents("beforeadd", "add", "addfailed")
    },
    randomNumber: function() {
        return new Date().getTime()
    }
});
executable
  • 2,788
  • 3
  • 16
  • 39

2 Answers2

1

Return that from function from another function

function a(){console.log("ds")}
function b(){return a();}
b();
ellipsis
  • 11,498
  • 2
  • 13
  • 33
0

this refers to the current scope which in this case is changed. Basically it means that the randomNumber() should exist on all objects in order to be able to do

this.randomNumber();

I suggest you add this function to the global/window scope so than you can simply say

randomNumber();
Rob
  • 2,065
  • 1
  • 18
  • 29