2

I've tried to search for an answer to my question, but I'm starting to think that, given the lack of results, I'm obviously not expressing the question properly. With that in mind, apologies if the title of this post is misleading, I am still very much learning.

I have a simplified version of my code below

var testData = ['a', 'b']

    var addReceiver = (function () {
        dataReceiver = function (dataInput) {
            t = this
            t.data = dataInput;
            console.log(t.data);
            t.interval = setInterval(function () {
                t.update();
            }, 1000);
            t.stopUpdate = function () { clearInterval(t.interval); };
            t.update = function () {
                //t.data = dataInput;
                console.log(t.data);
            };
        };
    })()

    var testLogger = new dataReceiver(testData);
</script>

The behaviour that I wish to emulate is when I type into the console

testData = ['c','d','e']

for testLogger to log the array ['c','d','e'] every second rather than ['a','b'].

I could change

t.data = dataInput to t.data = testData

to achieve this, but that would obviously be pointless, or else every new instance I created of dataReceiver would have a reference to the same data input.

So, my question is, how would I need to change the code to provide the testLogger vairable (or any instance of dataReceiver) access to a variable outside of its local scope?

1 Answer 1

2

Use the object that you have created instead of accessing the global variable,

var testLogger = new dataReceiver(testData);
testLogger.data = [1,2,3,4];

Now you will be able to print the newly injected data. I mean the setInterval will print the updated value.

Sign up to request clarification or add additional context in comments.

3 Comments

Yes, in theory thats an option. But I may have 3 variables which take testData as an input and a 4th which uses another variable egvar testLogger1 = new dataReceiver(testData); var testLogger2 = new dataReceiver(testData); var testLogger3 = new dataReceiver(testData); var testLogger4 = new dataReceiver(otherData); and in this instance it wouldn't be practical to update in the way you've suggested. The data being passed into testData is coming via a websocket and could update at any point, when it does, I'd like testLogger to use these updated values on its next refresh. Does that make sense?
@cb1210 For that I would suggest you to hold those objects in an array and whenever the value has to be changed, you need to iterate over that array and change the object's property value one by one.
OK, So the answer then is that I can't, but I could possibly add some logic into the websocket onmessage event Listener if I could somehow get a list of objects that are currently using that websocket as their dataInput? Thank you for your help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.