/ *配列の定義* /
VaRのARR:番号[] = [1、2、3]。
VaRのARR1:配列<番号> = [1、2、3]。
VaRのARR2:[文字列、数] = [ 'この文字列で'、1]。
/ *列挙型* /
列挙型のステータス{
成功= 200、
誤差= 404
}
状態を聞かせて:状態= Status.success。// 200
ステータス= Status.error;:statu2をしましょう // 404
/ *
列挙型カラー{
青、
赤、
黄
}
blueIndexてみましょう:カラー= Color.blueを。// 0
redIndexてみましょう:カラー= Color.redを。// 1
* /
列挙型カラー{
青、
、4 =赤
黄
}
blueIndexてみましょう:カラー= Color.blueを。
console.log(blueIndex)
redIndexてみましょう:カラー= Color.redを。
console.log(redIndex)// 4
yellowIndexてみましょう:カラー= Color.yellowを。
console.log(yellowIndex)// 5
/ *ボイド*型/
関数の実行():無効{
console.log( "私はruningています")
}
関数ISNUMBER():数{
124を返します。
}
/ *関数の型* /
// ES5の種類の関数
機能食べます(){
リターン「食べます」;
}
VaRのeat2 =関数(){
リターン「食べます」
}
機能// TSの種類
function eat3(): string {
return 'eating';
}
// 定义传参数类型
function eat4(name: string, many: number): string {
return `我吃饱了${name}`;
}
// 默认参数
function eat5(name: string, many: number = 4): string {
return `我吃饱了${name}`;
}
// 三点运算符
function sum(...result: number[]) {
let sum;
for (let index = 0; index < result.length; index++) {
sum += result[index];
}
return sum;
}
sum(1,2,3); // 6
/* es5中对象的继承 */
/*
function Person() {
this.name ='fasd';
this.age = 4;
this.worker = function() {
console.log('工作');
}
}
function Web() {
Person.call(this); // 对象冒充继承法(只能继承对象属性和方法不能继承原型链上属性和方法)
}
var w = new Web();
w.worker();
*/
function Person(name,age) {
this.name =name;
this.age = age;
this.worker = function() {
console.log('工作');
}
}
function Web(name,age) {
Person.call(this,name,age);
}
Web.prototype = Person.prototype; // Web.prototype = new Person();
var w = new Web('fsdf', 34);
w.worker();
/* 类的继承 */
class animal {
name:string;
constructor( name) {
}
look() {
console.log('看世界')
}
}
class Person2 extends animal{
name: string;
age: number;
constructor(name,age) {
super(name)
this.name = name;
this.age = age;
}
eat () {
console.log(`我吃${this.age}个蛋`)
}
}
var p = new Person2('df', 22);
alert(p.look()) // 看世界