1

I would love a clear explanation as to better understand what's going on here?

I can create what look like primitive types with Object. It looks like a number but not quite for a string. I was under the impression that Object() was used to create all objects in js (ie of type object) but not primitive types, like number, string boolean.

enter image description here

Yunti
  • 6,761
  • 12
  • 61
  • 106
  • it's the same wrapper all primitives use to provide methods. – dandavis May 11 '17 at 16:58
  • As stated [here on MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object) *The Object constructor creates an object wrapper.*. – Heretic Monkey May 11 '17 at 17:02

4 Answers4

1

The notion of primitives and objects is slightly implementation dependent. But you can think of it like this:

Primitives are not objects. They have their own special semantics, such as the string "hello" is the same no matter how many times you create one. However, the object new String("hello") should return a new string every time.

In many implementations, there is auto-boxing and unboxing of primitives when they need to act like objects and vice-versa. I would suggest reading this question for more details: What is the difference between string literals and String objects in JavaScript?

Community
  • 1
  • 1
AnilRedshift
  • 7,937
  • 7
  • 35
  • 59
  • Thanks for the link that's very helpful to understand the lifetime that Pankaj Shukla mentioned above. – Yunti May 11 '17 at 19:09
1

There are times when a primitive type (Boolean, Number or String) needs to be converted to object albeit temporarily.

Look at this example:

var str = 'javascript';
str = str.substring(4);// You get "script" but how?

How can you call a function on something that is not an object? The reason is that primitive value is wrapped into Object(String) on which substring() method is defined. If it was not done, you would not be able to call the method in the primitive type. Such wrappers are called Primitive Wrappers. Also, it doesn't mean that str has now become an object. So, if you said:

str.myProp = 10;
and then console.log(str.myProp);//You would get undefined

The difference between a reference type and a primitive wrapper object is lifetime. Primitive wrappers die soon.

So you can think of

var str = 'javascript';
str = str.substring(4);// You get "script" but how?

As these lines:

var str = new String(“javascript”);
str = str.substring(4);
str = null;

Now coming to what you are doing:

number = Object(5);

Here you are wrapping a number primitive into an object. It is as if you had written: number = new Number(5);

Here, Object(5) is behaving as a factory and depending on the type of the input(number, string), it gives back object by wrapping primitive value into it.

So, Object(5) is equivalent to new Number(5) and Object('test') is as if saying new String('test').

Hope this helps!

Pankaj Shukla
  • 2,657
  • 2
  • 11
  • 18
0
5.toString();

As you can see Number is an object. But number is not. In some cases primitives are wrapped by objects to implement object like behaviours...

5 // primitive type
Object(5) // wrapper of that primitive type
5.toString() === Object(5).toString() //...
Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151
0

You can use Object.prototype.valueOf() to get [[PrimitiveValue]] 5 or "test".

var number = new Object(5);
var aString = new Object("test");
console.log(number.valueOf(), aString.valueOf());
guest271314
  • 1
  • 15
  • 104
  • 177