/* typeScript中的接口 - 1.屬性類接口 */ /* 接口的作用:在面向對象的編程中,接口是一種規范的定義,它定義了行為和動作的規范,在程序設計里面,接口起到一種限制和規范的作用。接口定義了某一批類所需要遵守的規范,接口不關心這些類的內部狀態數據,也不關心這些類里方法的實現細節,它只規定這批類里必須提供某些方法,提供這些方法的類就可以滿足實際需要。 typescrip中的接口類似於java,同時還增加了更靈活的接口類型,包括屬性、函數、可索引和類等。 定義標准。 */ // 1、屬性接口 對json的約束 //ts中定義方法 /* function printLabel():void { console.log('printLabel'); } printLabel(); */ /* ts中定義方法傳入參數 function printLabel(label:string):void { console.log('printLabel'); } printLabel('hahah'); */ /* ts中自定義方法傳入參數,對json進行約束 */ /* function printLabel(labelInfo:{label:string}):void { console.log('printLabel'); } printLabel('hahah'); //錯誤寫法 printLabel({name:'張三'}); //錯誤的寫法 printLabel({label:'張三'}); //正確的寫法 */ //對批量方法傳入參數進行約束。 //接口:行為和動作的規范,對批量方法進行約束 //就是傳入對象的約束 屬性接口 // interface FullName{ // firstName:string; //注意;結束 // secondName:string; // } // function printName(name:FullName){ // // 必須傳入對象 firstName secondName // console.log(name.firstName+'--'+name.secondName); // } // // printName('1213'); //錯誤 // var obj={ /*傳入的參數必須包含 firstName secondName*/ // age:20, // firstName:'張', // secondName:'三' // }; // printName(obj) // 接口:行為和動作的規范,對批量方法進行約束 // interface FullName{ // firstName:string; //注意;結束 // secondName:string; // } // function printName(name:FullName){ // // 必須傳入對象 firstName secondName // console.log(name.firstName+'--'+name.secondName); // } // function printInfo(info:FullName){ // // 必須傳入對象 firstName secondName // console.log(info.firstName+info.secondName); // } // var obj={ /*傳入的參數必須包含 firstName secondName*/ // age:20, // firstName:'張', // secondName:'三' // }; // printName(obj); // printInfo({ // firstName:'李', // secondName:'四' // }) //接口 :可選屬性 // interface FullName{ // firstName:string; // secondName:string; // } // function getName(name:FullName){ // console.log(name) // } // //參數的順序可以不一樣 // getName({ // secondName:'secondName', // firstName:'firstName' // }) // interface FullName{ // firstName:string; // secondName?:string; // } // function getName(name:FullName){ // console.log(name) // } // getName({ // firstName:'firstName' // }) /* $.ajax({ type: "GET", url: "test.json", data: {username:$("#username").val(), content:$("#content").val()}, dataType: "json" }); */ interface Config{ type:string; url:string; data?:string; dataType:string; } //原生js封裝的ajax function ajax(config:Config){ var xhr=new XMLHttpRequest(); xhr.open(config.type,config.url,true); xhr.send(config.data); xhr.onreadystatechange=function(){ if(xhr.readyState==4 && xhr.status==200){ console.log('chengong'); if(config.dataType=='json'){ console.log(JSON.parse(xhr.responseText)); }else{ console.log(xhr.responseText) } } } } ajax({ type:'get', data:'name=zhangsan', url:'http://a.loaderman.com/api/productlist', //api dataType:'json' })