0

While creating a new object, why does constructor not return object with the name of the newly created object

In the following, the output is cons{x: "hi"} object, why not myobj {x: "hi"}?

function cons() {
  this.x = "hi";
  return null;
}

var myobj = new cons();
console.log(myobj);

This article, says that "It returns the newly created object,unless the constructor function returns a non-null object reference." So it should return myobj{x: "hi"} since the above constructor is returning null.

Community
  • 1
  • 1
b Tech
  • 412
  • 4
  • 14

2 Answers2

2

In the following, the output is cons{x: "hi"} object, why not myobj {x: "hi"}?

What you see will vary depending on which console implementation you're using, but some (like Chrome's) will indeed show you that.

The cons there is to tell you what type of object it is,1 not what variable the object's reference is stored in. It's just so you can readily distinguish between the different types of objects in your code when looking at the dev tools.

As for why: Remember that console.log has no idea where the value it got came from; it doesn't know the value came from a variable called myobj. When you do console.log(myobj), the value of myobj is read, and then that value is passed into console.log. So it couldn't print the variable name even if the designers of it wanted it to.

If you want to see the variable name as well, put it in the console.log call:

console.log("myobj", myobj);

Example:

function cons() {
  this.x = "hi";
  return null;
}

var myobj = new cons();
console.log("myobj", myobj);

This article, says that "It returns the newly created object,unless the constructor function returns a non-null object reference." So it should return myobj{x: "hi"} since the above constructor is returning null.

You're quite understandably confusing two unrelated things. The result of calling a constructor function with new, and the output of passing an object reference into console.log.

In your code, var myobj = new cons(); does indeed result in the new object created by new being stored in myobj (the return null; has no effect on it). What that quote is trying to say is that something like this wouldn't:

function cons() {
  this.x = "hi";
  return {hey: "I'm a completely different object"};
}

var myobj = new cons();
console.log(myobj);

In that code, cons has overridden the default behavior of new by returning a non-null object reference to something other than this. So the result of new cons is a reference to that other object, not the one new created. It's not something constructors usually need to do, but sometimes (for instance) you want something that looks like a constructor to actually provide a previous instance instead of the new object.


1 Loosely speaking; objects don't really have type in JavaScript, just a prototypical inheritance lineage.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
1

Objects have no name in general. What you are seeing is an implementation-dependent stringification of the object. Firefox console would show

Object { x: "hi" }

Chrome presumably shows the name of the constructor of that object. Functions do have a name.

Oriol
  • 274,082
  • 63
  • 437
  • 513