javascript函数中显式和隐式返回之间的任何区别?

Noc*_*tis 4 javascript return function

当我从函数显式返回而隐式返回时有什么区别吗?

这是困扰我ATM的代码:

function ReturnConstructor(arg){
    // Private variable. 
    var privateVar = "can't touch this, return ctor.";

    // This is what is being exposed to the outside as the return object
    return {
        print: function() {
            console.log("ReturnConstructor: arg was: %s", arg);
        }
    };
}

function NormalConstructor(arg){
    // Private variable. 
    var privateVar = "can't touch this, normal ctor";

    // This is what is being exposed to the outside as "public"
    this.print = function() {
            console.log("NormalConstructor: arg was: %s", arg);
        };
}

var ret = new ReturnConstructor("calling return");
var nor = new NormalConstructor("calling normal");
Run Code Online (Sandbox Code Playgroud)

两个对象('ret'和'nor')对我来说都是一样的,我想知道是否只有个人喜欢写过我到目前为止读过的任何一篇文章的人,或者是否有任何隐藏的陷阱.

Poi*_*nty 6

当你使用时new,有一个隐含的值.当你使用时new,没有.

当一个被调用的函数new返回一个对象时,那就是new表达式的值.当它返回其他内容时,将忽略该返回值,并且隐式构造的对象是表达式的值.

所以,是的,可能会有所不同.