Comparing Classes: C# 6, ES6/ES2015 JavaScript, and TypeScript
Comparing Classes: C# 6, ES6/ES2015 JavaScript, and TypeScript
Classes are a useful construct typically associated with object-oriented programming.
In C# classes are a first-class construct of the language. Classes were introduced a bit later for JavaScript. Classes in JavaScript are simply syntactic sugar.
C# 6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public class Person
{
private readonly SomeDependency _someDependency;
public Person(SomeDependency someDependency)
{
_someDependency = someDependency;
}
public string GivenName { get; set; }
public string FamilyName { get; set; }
public string Name => $"{GivenName} {FamilyName}";
}
public class SomeDependency { }
// Usage:
var person = new Person(new SomeDependency());
person.GivenName = "Ken";
person.FamilyName = "Dale";
Console.WriteLine(person.Name); // Ken Dale
JavaScript (ES6 / ES2015)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Person {
constructor(someDependency) {
this.someDependency = someDependency;
}
get name() { return `${this.givenName} ${this.familyName}`; }
}
class SomeDependency {
}
// Usage:
const person = new Person(new SomeDependency());
person.givenName = "Ken";
person.familyName = "Dale";
console.log(person.name); // Ken Dale
TypeScript v1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Person {
constructor(public someDependency: SomeDependency) {
}
givenName: string;
familyName: string;
get name(): string { return `${this.givenName} ${this.familyName}`; };
}
class SomeDependency { }
// Usage:
const person = new Person(new SomeDependency());
person.givenName = "Ken";
person.familyName = "Dale";
console.log(person.name); // Ken Dale
I hope this brings clarity by comparing these different languages.
This post is licensed under CC BY 4.0 by the author.