Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
95
rated 0 times [  96] [ 1]  / answers: 1 / hits: 7193  / 11 Years ago, thu, january 9, 2014, 12:00:00

Is there any difference when I explicitly return from a function vs. implicitly return?



Here's the code that puzzles me 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);


Both objects ('ret' & 'nor') seem the same to me, and I'm wondering if it's only personal preference of whomever wrote whichever article I've read so far, or if there's any hidden traps there.


More From » function

 Answers
5

When you use new, there's an implicit value. When you don't use new, there isn't.



When a function called with new returns an object, then that's the value of the new expression. When it returns something else, that return value is ignored and the object implicitly constructed is the value of the expression.



So, yes, there can be a difference.


[#48825] Thursday, January 9, 2014, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
breap

Total Points: 606
Total Questions: 96
Total Answers: 108

Location: Djibouti
Member since Sun, Feb 27, 2022
2 Years ago
breap questions
Thu, Jun 24, 21, 00:00, 3 Years ago
Wed, Mar 18, 20, 00:00, 4 Years ago
Mon, Oct 7, 19, 00:00, 5 Years ago
;