1. On this case, how can I return value 5 that's I want?
With a simple function and with direct call without the new
operator.
function add(a, b) {
return a + b;
}
document.write(add(2, 3));
2. In the function Add, I explicitly return 5, why do I get return Add instance?
3. Does new Function() only return this instance?
You get the instance, because of the new
operator.
3.The object returned by the constructor function becomes the result of the whole new expression. If the constructor function doesn't explicitly return an object, the object created in step 1 is used instead. (Normally constructors don't return a value, but they can choose to do so if they want to override the normal object creation process.)
According to this, a new instance does not return a primitive data type. You can only overwrite the return value (this
) with an object.