classHamburger{ constructor() { // This is the constructor. } listToppings() { // This is a method. } }
Object
一个对象是通过使用 new 关键字创建的一个类的实例。
1 2
let burger = new Hamburger(); burger.listToppings();
Refresher on ‘this’
在 Javascript 中, this 的指向一直是个经久不衰的话题。传统的基于类的语言中的 this 关键字通常指向类的当前实例。然而在 Javascript 中, this 则指向当前调用的上下文,因此可能指向任何其他值而不是当前对象。在函数内部,this的值取决于函数被调用的方式。在严格模式下,如果 this未在执行的上下文中定义,那它将会默认为 undefined 。
JavaScript’s inheritance works differently from inheritance in other languages, which can be very confusing. ES6 classes provide a syntactic sugar attempting to alleviate the issues with using prototypical inheritance present in ES5. Our recommendation is still to avoid using inheritance or at least deep inheritance hierarchies. Try solving the same problems through delegation instead.
Constants and Block Scope Veribles
1 2 3 4 5 6 7 8
var i; for(i = 0; i < 10; i++) { var j = i; let k = i; }
ES6 also introduces the concept of a module, which works similar to other languages. Defining an ES6 module is quite easy: each file is assumed to define a module and we specify its exported values using the export keyword.
Loading ES6 modules is a little trickier. In an ES6-compliant browser you use the System keyword to load modules asynchronously. To make our code work with current browsers, however, we will use SystemJS library as a polyfill:
1 2 3 4 5 6 7 8 9 10
<scriptsrc="/node_module/systemjs/dist/system.js"></script> <script> var promise = System.import('app') .then(function() { console.log('Loaded!'); }) .then(null, function(error) { console.error('Failed to load:', error); }); </script>
TypeScript
Angular 2 is built in TypeScript
1
npm install -g typescript
Without TS:
1 2 3 4 5 6
functionadd(a, b) { return a + b; }
add(1, 3); // 4 add(1, '3'); // '13'
With TS:
1 2 3 4 5 6 7
functionadd(a: number, b: number) { return a + b; }
add(1, 3); // 4 // compiler error before JS is even produced add(1, '3'); // '13'
Types
boolean (true/false)
number integers, floats, Infinity and NaN
string characters and strings of characters
[] Arrays of other types, like number[] or boolean[]
{} Object literal
undefined not set
enum enumerations like { Red, Blue, Green }
any use any type
void nothing
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
let isDone: boolean = false; let height: number = 6; let name: string = "bob"; let list: number[] = [1, 2, 3]; let list: Array<number> = [1, 2, 3]; enum Color {Red, Green, Blue}; let c: Color = Color.Green; let notSure: any = 4; notSure = "maybe a string instead"; notSure = false; // okay, definitely a boolean
Underneath TypeScript is JavaScript, and underneath JavaScript is typically a JIT (Just-In-Time compiler). Given JavaScript’s underlying semantics, types are typically reasoned about by “shapes”. These underlying shapes work like TypeScript’s interfaces, and are in fact how TypeScript compares custom types like classes and interfaces.
Despite the fact that Action and NotAnAction have different identifiers, tsc lets us assign an instance of NotAnAction to a which has a type of Action. This is because TypeScript only really cares that objects have the same shape. In other words if two objects have the same attributes, with the same typings, those two objects are considered to be of the same type.
functionlog(prefix?: string) { return(target) => { // save a reference to the original constructor var original = target;
// a utility function to generate instances of a class functionconstruct(constructor, args) { var c : any = function () { returnconstructor.apply(this, args); } c.prototype = constructor.prototype; returnnew c(); }
// the new constructor behavior var f : any = function (...args) { console.log(prefix + original.name); return construct(original, args); }
// copy prototype so instanceof operator still works f.prototype = original.prototype;
In the example log is invoked using @, and passed a string as a parameter, @log() returns an anonymous function that is the actual decorator. The decorator function takes a class, or constructor function (ES5) as an argument. The decorator function then returns a new class construction function that is used whenever World is instantiated. This decorator does nothing other than log out its given parameter, and its target‘s class name to the console.