Typescript support classes for a simple getter and setter
//ts实现getter和setter //这个文件只能编译成es5 let passCode = 'Secret Password' class Employee { private _fullName: string get fullName(): string{ return this._fullName } set fullName(newName: string) { if(passCode && passCode == 'Secret Password'){ this._fullName = newName } else { console.error ('Error: Unauthorized update of employee!') } } } let employee = new Employee() employee.fullName = 'BOb smith' IF (employee.fullName) { the console.log (employee.fullName) // If passCode correctly, Smith output BOb }
Files compiled as follows
// TS achieve getter and the setter // This file can be compiled into ES5 var PASSCODE = 'Secret Password' ; var the Employee = / * * @class * / ( function () { function the Employee () { } Object.defineProperty (the Employee .prototype, "the fullName" , { GET: function () { return the this ._fullName; }, SET: function (newName) { IF (PASSCODE PASSCODE && == 'Secret Password' ) { the this._fullName = newName; } else { console.error('Error: Unauthorized update of employee!'); } }, enumerable: true, configurable: true }); return Employee; }()); var employee = new Employee(); employee.fullName = 'BOb smith'; if (employee.fullName) { console.log(employee.fullName); //如果passCode正确,输出BOb smith }
Static member class is created, these properties exist only in class by itself
// use the static properties of // calculated coordinate point to any point of the distance class the Grid { static orign = {X: 0, Y: 0} // static properties, may be used directly Scale: Number // defines the zoom ratio constructor (scale: Number) { the this .scale scale = // the scale passed this, new function must be passed when } // Create an instance method calculateDistanceFromOrign (Point: {X: Number; Y: Number}) { the let xDist point.x = - Grid.orign.x // static property can be used directly from the let yDist = point.y - Grid.orign.y // Pythagorean theorem return Math.sqrt (xDist * xDist + yDist * yDist) * the this . scale } } let grid1 = new Grid(1.0) let grid2 = new Grid(5.0) //勾3股4弦5 console.log(grid1.calculateDistanceFromOrign({x:3 , y:4}))//5 console.log(grid2.calculateDistanceFromOrign({x:3 , y:4}))//25
Compiled files
// use the static properties of // calculate the distance to any point in the coordinate system point var the Grid = / * * @class * / ( function () { function the Grid (scale) { the this .scale = scale; // the mass scale to this, new function must be passed when } // Create an instance method Grid.prototype.calculateDistanceFromOrign = function (Point) { var xDist = point.x - Grid.orign.x; // static property can be used directly var yDist point.y = - Grid.orign.y; // Pythagorean return the Math.sqrt (xDist yDist * + * xDist yDist) *the this .scale; }; Grid.orign = {X: 0, Y: 0}; // static properties, may be used directly return the Grid; } ()); var Grid1 = new new the Grid (1.0 ); var GRID2 = new new the Grid (5.0 ); // hook 3 Unit 4 chord. 5 the console.log (grid1.calculateDistanceFromOrign ({X: 3, Y: 4 })); the console.log (grid2.calculateDistanceFromOrign ({X: 3, Y: 4}) );
TypeScript abstract class
As another general abstract class derived class or a base class, generally can not be used directly
Department {class abstract name: String constructor (name: String) { the this .name = name } printName (): void { the console.log ( 'Department name' + the this .name) } abstract printMeeting (): void // embodied to achieve in a derived class, which is a signature } class Department {AccountingDepartment the extends constructor () { Super ( 'the Auditing Accounting AD' ) } // implement the abstract method printMeeting (): void { the console.log ( 'each of the Accounting Department Meets monday at 10am ') } GenterateReports (): void { the console.log ( '.. Reports Generating accounting' ) } } the let Department: Department // Department Department new new = () // error, can not create an instance of an abstract class Department = new new AccountingDepartment () department.printName () department.printMeeting () // department.genterateReports () // Department type is defined, there is no method of this example // output // Department name AD the Auditing Accounting // of the Accounting Department Meets each Monday 10am AT
Some advanced techniques like
//类的一些高级技巧 class Greeter { static standgrdGreeting = 'Hello , there' greeting: string constructor(message: string) { this.greeting = message } greet() { return 'Hello' + this.greeting } } let greeter: Greeter greeter = new Greeter('world') console.log(greeter.greet())//Helloworld
It can be seen that the above code static properties, there is an example method
// some advanced techniques like var the Greeter = / * * @class * / ( function () { function the Greeter (Message) { the this .greeting = Message; } Greeter.prototype.greet = function () { return 'the Hello' + the this .greeting; }; // beginning of a constructor to assign to the variable Greeter // after compiled static properties actually mounted on the assignment Greeter Greeter.standgrdGreeting = 'the Hello, there' ; return Greeter; } () ); var Greeter; Greeter =new Greeter('world'); console.log(greeter.greet());
After transformation
// Some kind of high-level skills class the Greeter { static standgrdGreeting = 'the Hello, there' the Greeting: String // modify optional parameter constructor (the Message? : String) { the this .greeting = the Message } the greet () { IF ( the this . Greeting) { return 'the Hello' + the this .greeting } the else { return Greeter.standgrdGreeting } } } the let Greeter: the Greeter Greeter = new new the Greeter () the console.log (greeter.greet ())// the Hello, there // If we make changes relatively standGreeting how to do it? The let greetMaker: typeof the Greeter = the Greeter greetMaker.standgrdGreeting = 'Hey, htere' the let greeter2: the Greeter = new new greetMaker () console.log (greeter2.greet ( )) // Hey, htere
Compiled
// some advanced techniques like var the Greeter = / * * @class * / ( function () { // modify the parameter is optional function the Greeter (Message) { the this .greeting = Message; } Greeter.prototype.greet = function ( ) { IF ( the this .greeting) { return 'the Hello' + the this .greeting; } the else { return Greeter.standgrdGreeting; } }; Greeter.standgrdGreeting= 'The Hello, there' ; return the Greeter; } ()); var Greeter; Greeter = new new the Greeter (); the console.log (greeter.greet ()); // the Hello, there // if we make changes to how the relative standGreeting ? do var greetMaker = the Greeter; greetMaker.standgrdGreeting = 'Hey, htere' ; var greeter2 = new new greetMaker (); console.log (greeter2.greet ());
As a class interface
//目前已废弃 class Point { x: number y: number } interface Point3D extends Point{ z: number } let point3d: Point3D = {x: 1 ,y: 2, z: 3}
2019-05-27 17:40:40