1

I was recently going through the typescript documentations

reference: http://www.typescriptlang.org/docs/handbook/typescript-in-5-minutes.html

in this documentation, there is similar typescript code related to section classes as follows

class Student {
  fullName: string;
  constructor(public firstName: string) {
    this.fullName = firstName;
  }
}
interface Person {
  firstName: string;
}
function greeter(person : Person) {
  return "Hello, " + person.firstName;
}
let user = new Student("Jane");
document.body.innerHTML = greeter(user);

I compiled this code with tsc filename.ts It's giving me a javascript compiled code as

var Student = /** @class */ (function () {
    function Student(firstName) {
        this.firstName = firstName;
        this.fullName = firstName;
    }
    return Student;
}());
function greeter(person) {
    return "Hello, " + person.firstName;
}
var user = new Student("Jane");
document.body.innerHTML = greeter(user);

In this generated javascript code I want to know this part:

var Student = /** @class */ (function () {
    function Student(firstName) {
        this.firstName = firstName;
        this.fullName = firstName;
    }
    return Student;
}());

I want someone to help me with good references for Javascript basics for this.

  • first line and last two lines are superfluous - basically the code is wrapped in an IIFE for no discernible reason – Bravo Oct 30 '18 at 07:42
  • Huh, I didn't realize I have a gold hammer in JavaScript, sorry Krishnan. Either way, what you're looking at is a immediately invoked function expression. Details can be found in https://stackoverflow.com/questions/8228281/what-is-the-function-construct-in-javascript. – Zeta Oct 30 '18 at 07:45
  • 1
    @Bravo — Since it is automated code generation, the IIFE is probably there to locally scope the methods (of which in this example there are zero) instead of making globals. – Quentin Oct 30 '18 at 07:45
  • @Quentin - I understand, I was explaining that particular code and trying to remove the mystifying part – Bravo Oct 30 '18 at 07:47
  • Hey thanks to all @Bravo, your keyword `IIFE` helped me – Rohan Tandel Oct 30 '18 at 07:59
  • @Zeta your reference helped me, however, I also found this as reference https://stackoverflow.com/questions/8774425/vs-in-javascript-closures that cleared me the concept – Rohan Tandel Oct 30 '18 at 08:04

0 Answers0