很多时候,我们过去到的数据是一个[object object],完全就不知道其内容是什么,我们就可以通过JSON.stringify(data)来分析里面的内容是什么。
alter( JSON.stringify(data) )
var num3 = 177.234 console.log("num3.toFixed() is "+num3.toFixed()) console.log("num3.toFixed(2) is "+num3.toFixed(2)) console.log("num3.toFixed(6) is "+num3.toFixed(6)) //num3.toFixed() is 177 //num3.toFixed(2) is 177.23 //num3.toFixed(6) is 177.234000
//文件Shape.ts namespace Drawing{ //export表示能让别的文件访问到 export interface IShape { draw() } } //文件Other 引入的文件只在一个命名空间下 /// <reference path = "Shape.ts" /> class Circle implements Drawing.IShape{ public draw() { console.log("画画") } } let cir = new Circle() cir.draw()
let c = { name:"asda", age:18 } console.log(c.name + "已经" + c.age + "岁了")
class Shape{ Area:number constructor(a:number) { this.Area = a } static print():void { console.log("输出了一段文字") } } class Circle extends Shape{ disp():void{ console.log("圆的面积为:" + this.Area) } } let cir = new Circle(100) cir.disp() Circle.print()
interface Animal{ name:string; wan: ()=>string; } let Dog:Animal = { name:"lala", wan: ():string=>{return "wangwang"} } console.log( Dog.wan() ) class Cat implements Animal{ name:string wan():string{ return this.name + "miaomiao" } constructor(giveName:string) { this.name = giveName } } let cat = new Cat("huahua") console.log( cat.wan() )
let str = new String("asdfghjkl"); console.log(str.length); //计算字符串长度 console.log(str.charAt(5)); //获取指定位置字符 console.log(str.concat("123456")); //连接字符串 console.log(str.indexOf("fgh")); //查找字符串的位置 console.log(str.split("f")) //用指定字符切割字符串为字符串数组 console.log(str.substring(1,5)) //获取子字符串 console.log(str.toUpperCase()) //转为大写 console.log(str.toLowerCase()) //转为小写
function funcName():string{ return "123"; } function funcName2(arg:number,arg1?:number){} let fun = function(arg:number,...arg1:Array<string>){} let func = ()=>{ console.log("Lambda"); }; func();
let result:number = 0; for(let i = 0;i<10;i++) { result += i; } console.log("结果:" + result) result = 0; let datas = [1,2,3,4,5,6,7,8,9]; datas.every((val,idx,array)=>{ result += val; return true; //返回true表示继续,返回false退出迭代 }) console.log("结果:" + result)
let n:number = 8; if(n>8) { console.log("大于8") } else if(n > 10) { console.log("大于10") } else { console.log("小于等于8") } switch(n) { case 8: console.log("等于8") break; case 10: console.log("等于10") break; default: console.log("默认") break; }