noimage

【Typescript 】extends. if number2, return error, how to create extends Class

class ErrorReport{
	public reportIt(){
		if(this.num == 2){
			return  "error";
		} else {
		 return "ok";
		}
	}
	constructor(public num:number){	
	}
}
var faf = new ErrorReport(2);
alert(faf.reportIt());//error

but this is diffcult to understand someone that saw source code,
then, extend class function,

class ErrorReport{
	public reportIt(){
		alert(this.errorNumber);
	}
	constructor(public errorNumber:number){	
	}
}
class RichErrorReport extends ErrorReport {
	public reportIt(){
		if(this.errorNumber == 1) alert("Memory Overflow");
		else if (this.errorNumber == 2) alert("File Not Found");
		else alert(this.errorNumber);
	}
}
var faf:ErrorReport = new RichErrorReport(2);
alert(faf.reportIt());

## duckTyping。Since the members are included in the type A in B, it does not matter even if the extra member is present. Operate

class A {
	a: number;
}
class B {
	a: number;
	b: number;
}
var x: A = new B();

## Definition of TypeScript can take advantage of the objects defined in the javascript side without

var A = (function(){
	function A (msg){
	 this.msg = msg;
	}
	return A;
})();

declare class A {
	msg: string;
	constructor(msg: string);
}
var a = new A("Message frome Space");
alert(a.msg);
(Visited 1 times, 1 visits today)

コメントを残す

メールアドレスが公開されることはありません。