search

Home  >  Q&A  >  body text

How does JS implement the file path of the called function?

Like the title, how to achieve it if possible? The following is the code. The requirement is that it can only be implemented in function foo.

// "/lib/a.js"

export default function foo () {
    // code here
    return path
}

// "/src/pages/b.js"

import foo from "../../lib/a.js"
let str = foo()
console.log(str) // 成功打印本地文件路径

PHP中文网PHP中文网2825 days ago510

reply all(2)I'll reply

  • 天蓬老师

    天蓬老师2017-05-19 10:17:49

    export default function foo () {
        // code here
        return __dirname
    }

    reply
    0
  • 淡淡烟草味

    淡淡烟草味2017-05-19 10:17:49

        // "/lib/a.js"
        
        export default function foo () {
            // code here
            return path
        }
        
        // "/src/pages/b.js"
        
        var str = "../../lib/a.js"
        
        import foo from str
        foo()
        console.log(str) // 成功打印本地文件路径
    

    Please ignore any grammatical errors

    reply
    0
  • Cancelreply