let str:string="你好 ts"
let str1:string = "你好,typesrcipe"
let flag:boolean=true
flag=false
let a:number = 1234
console.log(a)
var arr=[1, "3232", false]
let arr1:number[]=[1,2,34,45]
console.log(arr1);
let arr2:Array<number>=[1,2,32,4545,34]
let arr3:[string,number,boolean] = ["ts", 2.3, true]
enum Flag {success=1, error=-1}
var f:Flag=Flag.success
console.log(f);
enum Color {red, blue=5, oragne}
var c:Color=Color.red
var c1:Color=Color.blue
var c2:Color=Color.oragne
console.log(c);
console.log(c1);
console.log(c2);
var num:number = 123;
var num1:any = 1324;
num1 = "string"
var oBox:any = document.getElementById('box')
oBox.style.color='red'
var num:number;
console.log(num);
var num11:undefined;
console.log(num11);
var num11:undefined | undefined;
console.log(num11);
var num2:number | null | undefined
console.log(num2);
function run(){
console.log('run');
}
run();
function run1():void{
console.log('run1');
}
var a3:undefined;
a3=undefined;
var b:null;
b=null;
var n:never;
n =(() =>{
throw new Error('错误')
})()
function run11(){
return 'run11';
}
run11()
var run22 = function(){
return 'run2'
}
run22();
function srun():string{
return 'srun'
}
var srun2 = function():string{
return 'srun2'
}
srun2();
function getInfo(name:string, age:number):string{
return `${name}----------${age}`;
}
getInfo("yicheng", 13)
var getInfo1=function(name:string, age:number):string{
return `${name}----------${age}`;
}
getInfo1("yicheng", 13)
function vrun():void{
console.log('vrun.');
}
function getInfo2(name:string, age?:number):string{
if(age){
return `${name}----------${age}`;
}else{
return `${name}----------年龄保密`;
}
}
getInfo2("yicheng")
getInfo2("yichengi", 23)
function getInfo22(name:string, age:number=22):string{
if(age){
return `${name}----------${age}`;
}else{
return `${name}----------年龄保密`;
}
}
getInfo22('yicheng')
getInfo22('yicheng',30)
function sum(a:number, b:number, c:number, d:number):number{
return a+b+c+d;
}
function sum1(...result:number[]):number{
var sum = 0;
for(var i = 0; i < result.length; i++){
sum += result[i];
}
return sum;
}
alert(sum1(1,2,2,33,23))
function sum11(a:number, b:number, ...result:number[]):number{
var sum = a+b;
for(var i = 0; i < result.length; i++){
sum += result[i];
}
return sum;
}
alert(sum1(1,2,2,33,23))
function css(config){
console.log("config");
}
function css(config, value){
console.log("config, value");
}
function getInfo3(name:string):string;
function getInfo3(age:number):string;
function getInfo3(str:any):any{
if(typeof str === 'string'){
return '我叫:' + str;
}else{
return '我的年龄是' + str;
}
}
alert(getInfo3('张三'))
alert(getInfo3(20))
setTimeout(function(){
alert('run')
}, 1000)
setTimeout(() =>{
alert('run')
})
function Person(){
this.name='张三';
this.age=20;
}
var p = new Person()
p.name;
p.age;
function Person(){
this.name='张三';
this.age=20;
this.run = function(){
alert(this.name + '在运动');
}
}
person.prototype.sex='男'
person.prototype.work=function(){
console.log(this.name + '工作');
}
var p = new Person();
alert(p.name)
p.run();
p.work();
Person.getInfo=function(){
alert('我是静态方法');
}
Person.getInfo();
function Web(){
Person.call(this);
}
var w = new Web();
w.run();
w.work();
function Web(){
}
Web.prototype = new Person();
var w = new Web();
w.run();
w.work();
function Person(name, age){
this.name = name;
this.age = age;
this.run = function(){
alert(this.name + '在运动');
}
}
Person.prototype.sex = '男'
Person.prototype.work=function(){
alert(this.name + '在工作')
}
function Web(name, age){
}
Web.prototype = new Person();
var w = new Web('赵四', 20)
w.run();
class Person{
name:String;
constructor(n:string){
this.name = n;
}
run():void{
alert(this.name)
}
}
var p = new Person('张三');
p.run();
class Person{
name:string;
constructor(name:string){
this.name = name;
}
getName():string{
return this.name;
}
setName(name:string):void{
this.name = name;
}
}
var p = new Person('张三');
alert(p.getName());
p.setName('李四')
alert(p.getName());
class Person{
name:string;
constructor(name:string){
this.name = name;
}
run():string{
return `${this.name}在运动`
}
}
var p = new Person('王五')
p.run();
class Web extends Person{
}
class Web extends Person{
constructor(name:string){
super(name);
}
}
var w = new Web('李四');
class Web extends Person{
constructor(name:string){
super(name);
}
run():string{
return `${this.name}在运动-子类`;
}
work(){
alert(`${this.name}在工作`)
}
}
var w = new Web('李四');
alert(w.run());
|