Better HN
topnewbestaskshowjobs
Log inSign up

sidthesloth

20 karma · joined Mar 2, 2016 (10y ago) · 7 submissions

Submissions

1.

TypeScript Quirks – String or string? Which one should I use?(dineshbalaji.in ↗)

2 points · by sidthesloth · 5y ago · discuss

2.

Understanding HTML Form Encoding

1 points · by sidthesloth · 8y ago · discuss

3.

ThreeFingerTapJS: JavaScript lib to open Safari like preview windows for URL's(github.com ↗)

1 points · by sidthesloth · 9y ago · discuss

comment 9y ago · link · parent

This a follow up post on how to replace 'that = this' assignments with arrow functions: https://dbwriteups.wordpress.com/2017/04/14/replacing-that-t...

comment 9y ago · link · parent

    var name = "Global Name";
    var obj = {
        name : 'Object Name',
        printName : function() {
            setTimeout(() => {
                console.log(this.name);
            }, 2000);
        }
    };
    obj.printName();    //prints "Object Name"
    var printNameReference = obj.printName;
    printNameReference(); //prints "Global Name"
You will see that "Global Name" will still be printed out. The reason is because arrow functions take the enclosing lexical scope, and the arrow function to setTimeout in the example is created each time the printName function is invoked. The printNameReference() invocation sets the scope of the printName function to global because of the way it is invoked and hence the arrow functions to setTimeout will also take this scope.

Changing the printName function itself as arrow function does not help either.

    var obj = {
        name : 'Object Name',
        printName : () => {
            setTimeout(() => {
                console.log(this.name);
            }, 2000);
        }
    };
The above code will again set the printName function to the enclosing global scope, because that is where obj is declared.

6.

What does ‘that = this’ in JavaScript mean?(dbwriteups.wordpress.com ↗)

41 points · by sidthesloth · 9y ago · 58 comments