typescript property type interfaces

/*

typeScript Interface - Interface class 1. Properties
  
*/


/*
Functions as an interface: the object-oriented programming, interfaces are defined in a specification that defines the behaviors and motions of the specification, in which the programming interface functions as a restriction and specifications. Interface defines a number of classes required to comply with the specification, the interface does not care about the internal state of these classes of data, do not care about the implementation details of these class methods, only the provisions of these class must provide some method, provided these methods the class will meet actual needs. The interface is similar to typescrip java, but also increased the more flexible interface types, including properties, functions, and the like can be indexed.

The definition of standards.

*/


// 1, the properties of the interface constraints on json



    // TS defined method 
    / *
        function printLabel():void {
            console.log('printLabel');
        }
        printLabel();
    */


    /*
    The method defined parameters passed ts

        function printLabel(label:string):void {
            console.log('printLabel');
        }

        printLabel('hahah');

    */



        /*
            ts custom parameter passing method, to constrain json

        */

        /*
        
        function printLabel(labelInfo:{label:string}):void {
            console.log('printLabel');
        }

        printLabel ( 'hahah'); // Error wording


        printLabel ({name: 'John Doe'}); // error wording


        printLabel ({label: 'John Doe'}); // correct wording
    */



// to batch process the incoming parameters constraints.


// Interface: code of conduct and actions, the constraints of the batch method



        // is the constraint property of the incoming object's interface 
        //   interface the FullName {

        //      firstName: String; // attention; end 
        //      secondName: String;

        // }

        // function printName(name:FullName){

        //      // incoming object must secondName firstName 
        //      the console.log (name.firstName + '-' + name.secondName); 
        // } 
        // // printName ( '1213'); // Error

        // var obj = {/ * parameter passed in must contain secondName * firstName / 
        //      Age: 20 is, 
        //      firstName: 'Single', 
        //      secondName: 'three' 
        // }; 
        // printName (obj)







//   Interface: code of conduct and actions, the constraints of the batch method

       

            // interface the FullName { 
            //      firstName: String; // note; End 
            //      secondName: String; 
            // }

            // function printName(name:FullName){
            //     // 必须传入对象  firstName  secondName
            //     console.log(name.firstName+'--'+name.secondName);
            // }



            // function printInfo(info:FullName){

            //      // incoming object must secondName firstName 
            //      the console.log (info.firstName info.secondName +); 
            // }

            // var obj = {/ * parameter passed in must contain secondName * firstName / 
            //      Age: 20 is, 
            //      firstName: 'Single', 
            //      secondName: 'three' 
            // }; 
            // printName (obj);


            // printInfo({
            //     firstName:'李',
            //     secondName:'四'
            // })





// Interface: an optional attribute




    // interface FullName{

    //     firstName:string;
    //     secondName:string;
    // }

    // function getName(name:FullName){

    //      the console.log (name) 
    // } 
    // // The order of parameters can be different 
    // 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;
}

// native js package 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){
            the 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.itying.com/api/productlist', //api
    dataType:'json'
})

 

Guess you like

Origin www.cnblogs.com/loaderman/p/11040355.html