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

163
Visualizações
Pass variable into function to be defined

EDIT 2: Why has this very specific question been marked as a duplicate of this very conceptual one: What's the difference between passing by reference vs. passing by value?. If someone else had the same question as me, they wouldn't end up with this one on Google by searching for it and - even if they did - it wouldn't answer their question.

EDIT: Thanks everyone for your help so far. Pretty difficult stuff to understand from my point of view. The reason I'm doing this is that I've set up a function which I'm calling multiple times and in which I define a variable (a unique one with each call). I need to be able to refer back to each unique variable afterwards. Here is my actual attempted code below. What would the right way to do this be?

let newSeq1
let newSeq2
function sequenceClip(sample, length, sequenceVariable) {
        let currentPosition = Tone.Transport.position
        let whenToStart
        if (currentPosition === '0:0:0') {
            whenToStart = '0:0:0'
        } else {
            const barToStartOn = +currentPosition.slice(0, currentPosition.indexOf(':'))
            whenToStart = `${barToStartOn + 1}:0:0`
        }
        sequenceVariable = new Tone.Sequence((time, note) => {
            sampler.triggerAttackRelease(note, '4m', time)
        }, [sample], length).start(whenToStart);
    }

    loop1.addEventListener('mousedown', () => {
        sequenceClip("C3", '1m', newSeq1)
    })
    loop2.addEventListener('mousedown', () => {
        sequenceClip("C#3", '4m', newSeq2)
    })

How do I pass a variable into a function in Javascript to be assigned a value. E.g.:

Why does the variable not get assigned the value 5? And what's the way around this?

let a
function defineVariable(var2beDefined) {
   var2beDefined = 5
}
defineVariable(a)

console.log(a === 5)

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Reassigning an identifier, by itself, never has any side-effects (in most circumstances) - the only change resulting from someIdentifier = someNewValue will be when other parts of the code reference that same someIdentifier.

If you want to pass in something to be assigned to, pass in a function which, when called, assigns to the outer variable.

let a;
function defineVariable(callback) {
  callback(5);
}
defineVariable(newVal => a = newVal);

console.log(a === 5)

The only time that assigning to an identifier will have side effects is if:

  • Inside a function with a simple argument list, you reassign one of the parameters (in which case the arguments object will be changed as well)
  • You're using ES6 modules, and you reassign an identifier that's being exported, in which case other modules that import it will see the change as well
about 4 years ago · Juan Pablo Isaza Relatório

0

You would typically write an initializer function that returns a value and assign that return value to the relevant global variable. For example:

let newSeq1
let newSeq2

function sequenceClip(sample, length, sequenceVariable) {
    let currentPosition = Tone.Transport.position
    let whenToStart

    if (currentPosition === '0:0:0') {
        whenToStart = '0:0:0'
    } else {
        const barToStartOn = +currentPosition.slice(0, currentPosition.indexOf(':'))
        whenToStart = `${barToStartOn + 1}:0:0`
    }

    return new Tone.Sequence((time, note) => {
        sampler.triggerAttackRelease(note, '4m', time)
    }, [sample], length).start(whenToStart);
}

loop1.addEventListener('mousedown', () => {
    newSeq1 = sequenceClip("C3", '1m')
})

loop2.addEventListener('mousedown', () => {
    newSeq2 = sequenceClip("C#3", '4m')
})

Note that both newSeq1 and newSeq2 will be undefined until the first mousedown/mouseup events.

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