7

I'm trying to find in the docs / understand this behavior for the following code :

I saw this piece of code here:

 function f(){ return f; }
 new f() instanceof f;          //false

This is because ( from what i've read) :

When the constructor returns an object, the new operator will yield the returned object

So since f is a function - the new operator will yield the returned object which is f in this case

So : new f() === f

Hence : f instanceof f//false.

Question :

I 'm searching for this behaviour description in the docs , but couldn't find it.

I only found partial answer in mdn :

enter image description here

However - looking at the docs (which is what I really after) :

All it say is :

enter image description here

It doesn't mention the cases where the constructor return object or not( i'm sure i'm missing it)

Question: Where does in the docs that behavior is explained ?

nb ,

I know that constructor function should not (generally) return anything , this question is for knowledge.

nb2 :

example for this behaviour :

var z = {a: 2}; 
function g() { return z; } 
var x = new g(); 
x === z;  //true

Here, x is actually equal to z, down to the identity!

2
  • Can you please highlight your actual question? Commented Mar 20, 2014 at 15:36
  • @thefourtheye Im looking the reference in the docs. Commented Mar 20, 2014 at 15:36

3 Answers 3

7

That's because this behavior is a property of the internal [[Construct]] method, not new:

1. Let obj be a newly created native ECMAScript object.
[...]
8. Let result be the result of calling the [[Call]] internal property of F, providing obj as the this value and providing the argument list passed into [[Construct]] as args.
9. If Type(result) is Object then return result.
10. Return obj.

F is the function that is called via new (f in your case). Since f returns an object (step 8), it is returned (step 9). If it was not an object, the object in step 1 would be returned (step 10).

new simply returns what [[Construct]] returns:

5. Return the result of calling the [[Construct]] internal method [...]

Sign up to request clarification or add additional context in comments.

3 Comments

[[Construct]] returns what? it's just f no ?
I quoted that part in my answer. Either it returns what the function F returned ("Let result be the result of calling the [[Call]] internal property of F") or the object created in the first step ("return obj"). And yes, in your example it would be f, because that's what the function returns (step 8 and 9).
[[Construct]] is the name of the internal method. --- Sorry can't read, ignore this comment.
3

What you're looking for is found here. The documentation of the NewExpression you reference above specifies that it

returns the result of calling the [[Construct]] internal method

The spec for the [[Construct]] method is what you need.

Comments

2

Page 100 here:

... 8 Let result be the result of calling the [[Call]] internal property of F, providing obj as the this value and providing the argument list passed into [[Construct]] as args.

9 If Type(result) is Object then return result.

10 Return obj.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.