"0 to 100 ... real quick" - Drake
TypeScript
var Person = function (name) {
this.name = name;
};
Person.prototype.talk = function () {
console.log("Hi my name is " + this.name);
};
class Person {
constructor(name) {
this.name = name;
}
talk() {
console.log("Hi my name is " + this.name);
}
}
var Person = (function () {
function Person(name) {
this.name = name;
}
Person.prototype.talk = function () {
console.log("Hi my name is " + this.name);
};
return Person;
})();
function AnnoyingPerson() {
Person.apply(this, arguments);
};
AnnoyingPerson.prototype = Object.create(Person.prototype);
AnnoyingPerson.prototype.constructor = AnnoyingPerson;
AnnoyingPerson.prototype.talk = function () {
console.log("HI MY NAME IS " + this.name.toUpperCase());
};
class AnnoyingPerson extends Person {
talk() {
console.log("HI MY NAME IS " + this.name.toUpperCase());
}
}
var AnnoyingPerson = (function (_super) {
__extends(AnnoyingPerson, _super);
function AnnoyingPerson() {
_super.apply(this, arguments);
}
AnnoyingPerson.prototype.talk = function () {
console.log("HI MY NAME IS " + this.name.toUpperCase());
};
return AnnoyingPerson;
})(Person);
function Troll() {}
Object.defineProperty(Troll.prototype, "message", {
get: function () {
return "You mad bro?";
},
set: function (msg) {
console.log("Cool message bro ...");
}
});
class Troll {
get message() {
return "You mad bro?";
}
set message(msg) {
console.log("Cool message bro ...");
}
}
var Troll = (function () {
function Troll() {
}
Object.defineProperty(Troll.prototype, "message", {
get: function () {
return "You mad bro?";
},
set: function (msg) {
console.log("Cool message bro ...");
},
enumerable: true,
configurable: true
});
return Troll;
})();
function DoSomethingCool() {
// no ...
}
function DoSomethingCool() {
// no ...
}
function DoSomethingCool() {
// no ...
}
function Fetcher () {};
Fetcher.prototype.massageIt = function (data) {
return data;
};
Fetcher.prototype.fetchStuff = function () {
var _this = this;
return getJSON("/stuff").then(function (data) {
return _this.massageIt(data.data);
});
};
function Fetcher () {}
Fetcher.prototype.massageIt = function (data) {
return data;
};
Fetcher.prototype.fetchStuff = function () {
return getJSON("/stuff").then((data) => {
return this.massageIt(data.data);
});
};
function Fetcher() {
}
Fetcher.prototype.massageIt = function (data) {
return data;
};
Fetcher.prototype.fetchStuff = function () {
var _this = this;
return getJSON("/stuff").then(function (data) {
return _this.massageIt(data.data);
});
};
function Say(words) {
if (typeof words === 'undefined') {
words = "Hello World";
}
console.log(words);
}
function Say(words = "Hello World") {
console.log(words);
}
function Say(words) {
if (words === void 0) { words = "Hello World"; }
console.log(words);
}
function Add(/* ...values */) {
var slice = Array.prototype.slice,
values = slice.call(arguments);
return values.reduce(function (sum, value) {
return sum + value;
});
}
function Add(...values) {
return values.reduce((sum, value) => sum + value);
}
function Add() {
var values = [];
for (var _i = 0; _i < arguments.length; _i++) {
values[_i - 0] = arguments[_i];
}
return values.reduce(function (sum, value) {
return sum + value;
});
}
var name = "Ilia",
age = 24;
console.log("My name is " + name + ". I'm " +
age + " years old")
var name = "Ilia",
age = 24;
console.log(`My name is ${name}. I'm ${age} years old`);
var name = "Ilia", age = 24;
console.log("My name is " + name + ". I'm " +
age + " years old");
var MyModule = MyModule || {};
MyModule.Foo = "Bar";
MyModule.LogFoo = function () {
console.log(MyModule.Foo);
};
module MyModule {
export var Foo = "Bar";
export function LogFoo() {
console.log(Foo);
}
}
var MyModule;
(function (MyModule) {
MyModule.Foo = "Bar";
function LogFoo() {
console.log(MyModule.Foo);
}
MyModule.LogFoo = LogFoo;
})(MyModule || (MyModule = {}));
/**
* @param {string} msg Message to display.
*/
function Display(msg) {
console.log(msg);
}
/**
* @param {boolean} [loud=false] Greet "loudly".
*/
function Greet(loud) {
if (typeof loud === 'undefined') {
loud = false;
}
var msg = "Hello World";
console.log(loud ? msg.toUpperCase() : msg);
}
/**
* @param msg Message to display.
*/
function Display(msg: string) {
console.log(msg);
}
/**
* @param loud Greet "loudly".
*/
function Greet(loud: boolean = false) {
var msg = "Hello World";
console.log(loud ? msg.toUpperCase() : msg);
}
function Display(msg) {
console.log(msg);
}
function Greet(loud) {
if (typeof loud === 'undefined') {
loud = false;
}
var msg = "Hello World";
console.log(loud ? msg.toUpperCase() : msg);
}
class Person {
name: string;
age: number;
constructor(name: string, age: number) {
this.name = name;
this.age = age;
}
}
/**
* @class Person
* @param {string} name
* @param {number} age
*/
function Person(name, age) {
/**
* @name Person#name
* @type string
*/
this.name = name;
/**
* @name Person#age
* @type number
*/
this.age = age;
}
function Person(name, age) {
this.name = name;
this.age = age;
}
var person = new Person("Ilia", 24);
person.talks()
Uncaught TypeError: undefined is not a function
Property 'talks' does not exist on type 'Person'.
It's not about telling the computer how to store data, it's about validating your assumptions.
We're Not Writing C
interface UserData {
id: number;
name: string;
email: string;
lastSeen: Date;
}
function DoStuff(user: UserData): void {
// ...
}
function DoOtherStuff(user: UserData): void {
// ...
}
var w: any = window;
// or
var w = <any>window;
describe('User', function () {
describe('#getName()', function () {
it('should return a string', function () {
var user = new User("ilia"),
name = user.getName();
expect(name).to.be.a('string');
});
});
});