TS和JS互相调用

0.361字数 69阅读 10811

以下参考TS和JS互相调用(不用什么静态方法)

JS:<script>

function jsFun(a){
    return a;
}
</script>

TS:
class ImTS{
     function tsFun(a):any{
         return a;
     }
}

var imTS:ImTS = new ImTS();
window['imTS'] = imTS; //这步不能少

JS调用TS:
<script>
       window.imTS.tsFun();       
</script>

TS调用JS:
window['jsFun']();

以下参考关于js 调用 ts 的问题,在线等

module game {
        /**
         *
         * @author zhy
         *
         */
        export class TClass {
             
        private static s_instance: TClass = null;
 
        private platformInstance: TClass;
 
 
        public static getInstance(): TClass {
            if(!TClass.s_instance) {
                TClass.s_instance = new TClass();
                TClass.s_instance.init();
            }
 
            return TClass.s_instance;
        }
         
        public constructor() {
 
            if(TClass.s_instance) {
                throw new egret.error("TClass is a singleton class.");
            }
            this.init();
        }
 
                 
                private init(){
            window['platformInstance'] = TClass.s_instance; 
                }
                 
                 
                public jsCallFun(_arg:string):void{
                    console.log(_arg);
                }
        }
}

js:
window.platformInstance.jsCallFun("test!");
Ts中: window['platformInstance'] = TClass.s_instance; 注意一定要的。

在index的script里面写js函数和变量
ts调用参数是就是 window["参数名"],函数是window["函数名"]

推荐阅读更多精彩内容