search

Home  >  Q&A  >  body text

javascript - How can methods in ES6 modules call each other?

util/common.js

//定义一个ES6的模块,对外暴露很多公共的方法
export default {
    isEmpty(obj){
        for (var name in obj){
            return false;
        }
        return true;
    },
    isEmptyStr(str){
        if(str == null || str == undefined || str == ''){
            return true;
        }else{
            return false;                
        }
    },
    initUser(){
        //假设这里需要调用同模块中的isEmpty()来进行非空判断,该怎么调用???
    },
}
phpcn_u1582phpcn_u15822784 days ago451

reply all(2)I'll reply

  • 滿天的星座

    滿天的星座2017-05-19 10:41:01

    Treat it as an object, use this

    reply
    0
  • 过去多啦不再A梦

    过去多啦不再A梦2017-05-19 10:41:01

    • common.js

    function isEmpty(obj){
        for (var name in obj){
            return false;
        }
        return true;
    }
    function initUser(){
        isEmpty(obj)
        ...
    }
    export {isEmpty,initUser}
    • xxx.js

    import {isEmpty,initUser} from './common'

    And I feel like your common.js should be used as a universal script
    Just do it directlyimport './common.js'

    reply
    0
  • Cancelreply