Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

230
Visualizações
Path.join throws exception when given private variable as argument

I'm experimenting with Electron and I was trying to load an html file, and I store the path to that file in a private variable. Refer to code below.

However Path.join fails with this error:

undefined argument error.

BUT if I pass a string literal with the same value it works.

Non-working code:

import * as Electron from 'electron'
import * as Path from 'path'
import { Option, Some, None } from './option'

export default class Main
{
    private indexFilePath = 'index.html'
    private app: Electron.App
    private browserWindow: Option<Electron.BrowserWindow>

    constructor (app: Electron.App)
    {
        this.app = app
        this.browserWindow = new None()
        this.app.on('ready', this.onReady)
    }

    private onReady(): void
    {
        this.browserWindow = new Some(new Electron.BrowserWindow({
            width: 800,
            height: 600
        }))
        this.browserWindow.unwrap().loadFile(Path.join(__dirname, this.indexFilePath))
    }
}

Working code:

import * as Electron from 'electron'
import * as Path from 'path'
import { Option, Some, None } from './option'

export default class Main
{
    private app: Electron.App
    private browserWindow: Option<Electron.BrowserWindow>

    constructor (app: Electron.App)
    {
        this.app = app
        this.browserWindow = new None()
        this.app.on('ready', this.onReady)
    }

    private onReady(): void
    {
        this.browserWindow = new Some(new Electron.BrowserWindow({
            width: 800,
            height: 600
        }))
        this.browserWindow.unwrap().loadFile(Path.join(__dirname, 'index.html'))
    }
}

This doesn't make any sense to me and just increases my frustration with anything that has javascript in it. It feels like a whacky supersonic plane that could obliterate itself at any moment...

I did check if it was some weird typescript compilation error but no, doesn't matter which javascript version I choose the result is the same. Compiled code looks like this, I see nothing wrong with it either:

class Main {
    constructor(app) {
        this.indexFilePath = 'index.html';
        this.app = app;
        this.browserWindow = new option_1.None();
        this.app.on('ready', this.onReady);
    }
    onReady() {
        this.browserWindow = new option_1.Some(new Electron.BrowserWindow({
            width: 800,
            height: 600
        }));
        this.browserWindow.unwrap().loadFile(Path.join(__dirname, this.indexFilePath));
    }
}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Classic scoping error: just because you're pulling ready from this, does not mean that this.ready has any defined scope, it's just a bare function.

So, when you call this.app.on('ready', this.onReady), you are not telling JS to preserve the meaning of the this keyword at the time of declaration. Instead, any use of this that JS encounters once it runs your function will be "whatever the scope is at the time of execution", and when the event handling code triggers, that will be the global scope instead of your class instance.

Pretty simple solution: this is what we invented arrow functions for.

this.app.on('ready', () => this.onReady())

Done, when this function gets executed, its this is still the same as it was at declare time.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda