typescript namespace

/ * 
Namespace: 

    In case the code amount is large, the variables in order to avoid naming conflicts, may be functionally similar functions, classes, interfaces, etc. placed into the namespace 

    package with Java and .Net as namespace , TypeScript namespace code that can be wrapped up, only the external object needs to expose the external access. Foreign objects within the namespace exposed by the export keyword. 


Namespaces and modules differences: 

    namespace: internal modules, mainly for organizing your code to avoid naming conflicts. 

    Modules: ts referred to external modules, focusing on code reuse, a module may have multiple namespaces. 

* / 


Namespace {A 
    interface Animal { 
        name: String; 
        EAT (): void ; 
    } 
    Export the implements Animal Dog {class 
        name: String; 
        constructor (theName: String) { 
            the this .name = theName; 
        } 

        EAT () { 
            the console.log ( `$ { the this.name} 在吃狗粮。`);
        }
    }

    export class Cat implements Animal {
        name: string;
        constructor(theName: string) {
            this.name = theName;
        }

        eat() {
            console.log(`${this.name} 吃猫粮。`);
        }
    }   

}




namespace B{
    interface Animal {
        name: string;
        eat(): void;
    }
    export class Dog implements Animal {
        name: string;
        constructor(theName: string) {
            this.name = theName;
        } 

        EAT () { 
            the console.log ({$ ` the this .name} eating dog food .`); 
        } 
    } 

    Export the implements Cat Animal {class 
        name: String; 
        constructor (theName: String) { 
            the this .name = theName; 
        } 

        eAT () { 
            the console.log ({$ ` the this .name} eating cat food .`); 
        } 
    }    

} 


var C = new new B.Cat ( 'flower' ); 

c.eat ();

/ * 
Namespace: 

    In case the code amount is large, the variables in order to avoid naming conflicts, may be functionally similar functions, classes, interfaces, etc. placed into the namespace 

    package with Java and .Net as namespace , TypeScript namespace code that can be wrapped up, only the external object needs to expose the external access. Foreign objects within the namespace exposed by the export keyword. 


Namespaces and modules differences: 

    namespace: internal modules, mainly for organizing your code to avoid naming conflicts. 

    Modules: ts referred to external modules, focusing on code reuse, a module may have multiple namespaces. 

* / 

Import {A, B} from './modules/animal' ; 


var D = new new A.Dog ( 'black' ); 
d.eat (); 



var Dog = new new B.Dog ( 'flower' ); 
dog.eat ();
export namespace A{
    interface Animal {
        name: string;
        eat(): void;
    }
    export class Dog implements Animal {
        name: string;
        constructor(theName: string) {
            this.name = theName;
        }

        eat() {
            console.log(`${this.name} 在吃狗粮。`);
        }
    }

    export class Cat implements Animal {
        name: string;
        constructor(theName: string) {
            this.name = theName;
        }

        eat() {
            console.log(`${this.name} 吃猫粮。`);
        }
    }   

}

export namespace B{
    interface Animal {
        name: string;
        eat(): void;
    }
    export class Dog implements Animal {
        name: string;
        constructor(theName: string) {
            this.name = theName;
        }

        eat() {
            console.log(`${this.name} 在吃狗粮。`);
        }
    }

    export class Cat implements Animal {
        name: string;
        constructor(theName: string) {
            this.name =theName; 
        } 

        EAT () { 
            the console.log ({$ ` the this .name} .` cat food to eat); 
        } 
    }    

}

 

Guess you like

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