2

Playing around i found that the this object across callbacks on the same object stays the same. Is it wise to use this for scope control as follows.

process.stdin.once('data', (chunk) => {
    this.count = 0;
})

process.stdin.on('data', (chunk) => {
    this.count += chunk.length;
});

process.stdin.on('end', () => {
    console.log(`N of bytes ${this.count}`);
});

As I do not see this often, I'm wondering if anything is wrong with it or better methods of doing the same exist.

2 Answers 2

3

functions have their own scope, you could use that:

function doSomething(){
    var count = 0;

    process.stdin.once('data', (chunk) => {
        count = 0;
    })

    process.stdin.on('data', (chunk) => {
        count += chunk.length;
    });

    process.stdin.on('end', () => {
        console.log(`N of bytes ${count}`);
    });

}

If there's no need to call this function from elsewhere, and you just want it to execute you could make it an IIFE

(function(){
    var count = 0;

    process.stdin.once('data', (chunk) => {
        count = 0;
    })

    process.stdin.on('data', (chunk) => {
        count += chunk.length;
    });

    process.stdin.on('end', () => {
        console.log(`N of bytes ${count}`);
    });

})()

Note: Edited to correct the wrong char used for templated strings (` not ")

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

Comments

2

It works yes. The reason is you have no exterior closure, and your listeners all use es6 arrow functions so this is never bound to the callbacks. They all reference the global this.

A cleaner way might be to wrap them in a closure and reference a scoped variable.

The IIFE example in @Jamiec's answer would serve your purposes. Just edit the es6 template literal to use ` instead of " as a wrapper as follows:

(function(){
    var count = 0;

    process.stdin.once('data', (chunk) => {
        count = 0;
    })

    process.stdin.on('data', (chunk) => {
        count += chunk.length;
    });

    process.stdin.on('end', () => {
        console.log(`N of bytes ${count}`);
    });    
})()

7 Comments

Haha gotta love SO sometimes. Your answer basically says "Jamiec's answer is right" and you get the upvote. Anyway, +1 from me too!
Actually my answer was right before your answer was posted. I added your example, fixed it because it wouldn't of worked, and gave you credit. I also upvoted you...
My answer was upvoted before I referenced your example... Anyhow, have a great day!
@KyleRichardson (removed his comment, but I answer it anyway...) first of, your example don't scope "this" you are scoping "count".. and it would be possible to do in the same way as I edited your answer. Plus, my answer demanded way less code... it's also not a complete nock of of the correct answer already provided by Jamiec. gl hf...
I removed my comment because it was irrelevant. My answer actually explains what is happening, not just a code example. Your edit was also worthless in my opinion, which is why I declined it. Accept it and move on.
|

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.