113 Study Guide - Midterm Guide: Typescript, Ecmascript, West 8

6 views20 pages

Document Summary

Step: typescript part 4 arrow functions, creating of. One of the important features released in es6, and it is available in. Arrow function syntax has a fat arrow in it due to which the function is called an arrow function. Arrow function syntax: var nameoffunction = (params) => { Example: var scorecard = function () { this. score = 0; this. getscore = function () { settimeout(function () { console. log(this. score); // gives undefined. We created an anonymous function which has a property this. Score initialize to 0 and a method getscore which internally has a settimeout, and in 1 second it consoles this. score. Consoled value gives undefined though you have this. score defined and initialized. The issue here is with this keyword function inside settimeout has its own this, and it tries to refer the score internally, and since it is not defined, it gives undefined. Same can be taken care using arrow function.