Open conchincradle opened 2 years ago
Variables: boolean number string any let name: string = "Mike"; use let instead of var
console.log(name) node demo.js (it will be printed on command-line)
loop: let nums: number[] = [1,2,3,4]; for(let i=0;i<nums.length;i++){ console.log(nums[i]); }
for(let each of nums){
if(nums[i] == 1)
nums.push(3); growable as python's list
creating class
class Customer{
//properties
private firstName: string;
private lastName: string;
//constructors
constructor(theFirst: string, theLast: string){
this.firstName = theFirst;
this.lastName = theLast;
}
//getter/stter methods
}
let myCustomer = new Customer("Mike", "Levi");
del demo.js tsc --noEmitOnError demo.ts dir
// getter public getFirstName(): string{ return this.firstName; }
// setter public setFirstName(the First: string): void{ this.firstName = theFirst; }
public get firstName(): string{ return this.x; }
public set firstName(value: string){ this.x= value; }
public removed ok, public by default
compiler flag tsc --target ES5 --noEmitOnError demo.ts use tsconfig.json to store the compiler options { "compilerOptions":{ "noEmitOnError": true, "target": "es5" } }
tsc --init // generate a default tsconfig.json file
tsc // directly compile all the ts files
private _firstName: string;
get firstName(): string{ return this._firstName; }
set firstName(value: string){ this._firstName = value; }
class Customer{
constructor(private _firstName: string, private _lastName: string){ } // and write getter and setter
}
export class Customer{ }
import {Customer} from './Customer';
extend the superclasses, overriding
export class Shape{ constructor(private _x: number, private _y: number){ } getInfo(): string{ return `x=${this._x}, y = ${this._y}`; } }
import { Shape } from './Shape';
export class Circle extends Shape{ constructor (theX: number, theY: number, private _radius: number){ super (theX, theY); } getInfo((): string{ return super.getInfo() + `, radius=${this._radius}`
then a main write another ts file to import these ts class files
let myShape = new Shape(10, 15); let myCircle = new Circle(5, 10 ,20);
let the Shapes: Shape[] = [myShape, myCircle];
no instances like Shape, Computer, and abstract method should be implemented by concrete subclasses
a class can implement multiple interfaces
MicroSoft superset of JS
console.log("Hello World!"); tsc demo.ts -----------> demo.js node demo.js