conchincradle / my-web-shop-java

0 stars 0 forks source link

TypeScript #2

Open conchincradle opened 2 years ago

conchincradle commented 2 years ago

MicroSoft superset of JS

console.log("Hello World!"); tsc demo.ts -----------> demo.js node demo.js

conchincradle commented 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)

conchincradle commented 2 years ago

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){

conchincradle commented 2 years ago

if(nums[i] == 1)

nums.push(3); growable as python's list

conchincradle commented 2 years ago
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");
conchincradle commented 2 years ago

del demo.js tsc --noEmitOnError demo.ts dir

conchincradle commented 2 years ago

// getter public getFirstName(): string{ return this.firstName; }

// setter public setFirstName(the First: string): void{ this.firstName = theFirst; } image

conchincradle commented 2 years ago

Accessors

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

conchincradle commented 2 years ago

private _firstName: string;

get firstName(): string{ return this._firstName; }

set firstName(value: string){ this._firstName = value; }

conchincradle commented 2 years ago

shortcut syntax

class Customer{

constructor(private _firstName: string, private _lastName: string){ } // and write getter and setter

}

conchincradle commented 2 years ago

module

export class Customer{ }

import {Customer} from './Customer';

conchincradle commented 2 years ago

Inheritance

extend the superclasses, overriding

export class Shape{ constructor(private _x: number, private _y: number){ } getInfo(): string{ return `x=${this._x}, y = ${this._y}`; } }

conchincradle commented 2 years ago

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}`

conchincradle commented 2 years ago

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];

conchincradle commented 2 years ago

Abstract Class

no instances like Shape, Computer, and abstract method should be implemented by concrete subclasses

conchincradle commented 2 years ago

image

conchincradle commented 2 years ago

Interfaces

a class can implement multiple interfaces image