Dante383 Dante383 - 3 months ago 10
Javascript Question

Function in function in class

I have a class named Terminal in which I have a function constructor in which i have an anonymous function in which I would like to get access to the output() function in Terminal class. How can I do this?

Using this.output() throws an error. (this.output is not a function)

class Terminal
{
constructor(contentID, inputID, options)
{
if (!contentID || !inputID || !options)
{
return false;
}
this.cid = contentID;
this.iid = inputID;
if (!options['pcName'] || !options['username'])
{
return false;
}
this.pcname = (options['pcName'].replace('<', '&lt;')).replace('>', '&rt;');
this.username = options['username'];
this.version = '0.01';
// commands
this.commandsList = [];
this.commandsList['whoami'] = function(){ Terminal.output('lol') };

console.log('terminal.js version ' + this.version + ' initializing');
this.output('ja', true);
this.output('whoami', true);
this.output('i dont know bro', false, 'whoami');
}

output (text, prefix, cmdName) // cmdName is matters only when prefix is false
{
if (this.cid && text)
{
if (prefix == true)
{
text = this.username + '@' + this.pcname + ':~$ ' + text;
}
else if (cmdName)
{
text = cmdName + ': ' + text;
}
var con = document.getElementById(this.cid);
con.innerHTML = con.innerHTML + text + '<br>';
}
}

makeCommand (cmd)
{
if (cmd && cmd != '' && this.commandsList)
{
cmd = (cmd.replace('<', '&lt;')).replace('>', '&rt;');
if (this.commandsList[cmd])
{
console.log('Executing ' + cmd);
this.commandsList[cmd]();
}
}
}
}

Answer

I am not sure it's the answer, I think you need to define a new variable: that equal to this. Then call the method like so: that.Output()

class Terminal
{
    constructor(contentID, inputID, options)
    {
        if (!contentID || !inputID || !options)
        {
            return false;
        }
        this.cid = contentID;
        this.iid = inputID;
        if (!options['pcName'] || !options['username'])
        {
            return false;
        }
        this.pcname = (options['pcName'].replace('<', '&lt;')).replace('>', '&rt;');
        this.username = options['username'];
        this.version = '0.01';
        // commands
        this.commandsList = [];
        var that = this;
        this.commandsList['whoami'] = function(){ that.output('lol') };
 
        console.log('terminal.js version ' + this.version + ' initializing');
        this.output('ja', true);
        this.output('whoami', true);
        this.output('i dont know bro', false, 'whoami');
    }
   
    output (text, prefix, cmdName) // cmdName is matters only when prefix is false
    {
        if (this.cid && text)
        {
            if (prefix == true)
            {
                text = this.username + '@' + this.pcname + ':~$ ' + text;
            }
            else if (cmdName)
            {
                text = cmdName + ': ' + text;
            }
            var con = document.getElementById(this.cid);
            con.innerHTML = con.innerHTML + text + '<br>';
        }
    }
   
    makeCommand (cmd)
    {
        if (cmd && cmd != '' && this.commandsList)
        {
            cmd = (cmd.replace('<', '&lt;')).replace('>', '&rt;');
            if (this.commandsList[cmd])
            {
                console.log('Executing ' + cmd);
                this.commandsList[cmd]();
            }
        }
    }
}

Comments