Home > Mobile >  Pass variable into function to be defined
Pass variable into function to be defined

Time:02-04

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)

CodePudding user response:

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

CodePudding user response:

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.

  •  Tags:  
  • Related