Passing a pointer/reference to a variable as parameter

后端 未结 5 1754
情话喂你
情话喂你 2021-01-24 17:40

I know this question has been asked multiple times (yes, I did some research) but I can\'t see to find a solution that fits my needs.

What I have done so far:

相关标签:
5条回答
  • 2021-01-24 18:16

    In JavaScript values such as integers, strings, etc. are passed by value. If you want to pass a reference, you have to pass an object into the JavaScript function. (JavaScript objects are passed by reference)

    function adjustValues(referenceObject) {
        referenceObject.foo = 2;
        referenceObject.bar = "newValue";
    }
    
    referenceObject = {
      foo: 1,
      bar: "initialValue"
    };
    
    adjustValues(referenceObject);
    
    0 讨论(0)
  • 2021-01-24 18:20

    How can this be accomplished?

    Not with a variable. There are no "references to variables" in JS. I can see two simple solutions:

    1. pass a getter/setter function:

      function queue(getStatus) {
          …
          getStatus() // gets current value
          …
      }
      
      var executed = false;
      queue(function() { return executed; });
      
    2. pass an object with a property:

      function queue(status) {
           …
          status.executed // gets current value
          …
      }
      
      var status = {executed: false};
      queue(status);
      

    I have come up with a solution that involves executing a function only once per {whatever} milliseconds. This involves a variable set to true or false, if the function has already been executed in the {whatever} milliseconds.

    I cannot see the reason why this variable would need to be a parameter to the function, and be available (or even settable?) outside it. Just use a local variable inside queue.

    Btw, this functionality is known as debouncing, you don't have to write this yourself. Many implementations are already available on the web, sometimes as part of larger libraries. See for example What does _.debounce do?.

    0 讨论(0)
  • 2021-01-24 18:20

    Try the following example :

    'use strict';
    
    var observable = 0;
    
    function incObservable() {
    	++observable;
    	console.log('incObservable observable: '+observable);
    }
    function observe() {
    	console.log('observe observable: '+observable);
    }
    
    var observer = setInterval(observe, 100);
    
    setTimeout(function() {
    	incObservable();
    	setTimeout(function() {
    		incObservable();
    		setTimeout(function() {
    			incObservable();
    		}, 300);
    	}, 300);
    }, 300);
    
    setTimeout(function() {
    	// Stop obsever
    	clearInterval(observer);
    }, 1000);
    
    // observe observable: 0
    // observe observable: 0
    // incObservable observable: 1
    // observe observable: 1
    // observe observable: 1
    // observe observable: 1
    // incObservable observable: 2
    // observe observable: 2
    // observe observable: 2
    // observe observable: 2
    // incObservable observable: 3
    // observe observable: 3

    0 讨论(0)
  • 2021-01-24 18:24

    What you've described wanting to do doesn't immediately say "use a reference to a variable" to me (as Teemu points out, sounds like you want debouncing), but answering your question about references to variables...

    JavaScript doesn't have any form of references to variables (other than through closures, which might be problematic here). But you can readily do what you're talking about by just using an object and using a property on it. The property is the "variable."

    Simple example:

    function foo(obj) {
      var counter = 0;
      var timer = setInterval(function() {
        console.log("foo: " + obj.property);
        if (++counter === 5) {
          clearInterval(timer);
        }
      }, 500);
    }
    
    var o = {property: "unchanged"};
    // Give the "reference" to `property` to `foo`:
    foo(o);
    
    // Update it periodically while `foo` is doing its asynchronous thing
    setTimeout(function() {
      o.property = "update 1";
    }, 1000);
    setTimeout(function() {
      o.property = "update 2";
    }, 1700);

    0 讨论(0)
  • 2021-01-24 18:33

    why don't you use the setInterval function, it will do exactly what you want.

    Example:

    setInterval(function() { 
        // logic to be implemented
    }, delayInMilliseconds)
    
    0 讨论(0)
提交回复
热议问题