Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

typeof new String("aaa") === "object"? Everything is an object, but there are primitive types?

There will be a lot of questions needing clarifiction, so I'll try to mark them with numbers, to make it easier to respond to it.

Lately I have been studying javascript a lot. There is a subject about "everything is an object".

  1. In my "interpretation of javascript" that means everything has the "object" in the beginning of its prototype chain. Is this correct?

  2. But what about the primitive types (string, number, boolean, null, undefined)? Are they objects? I can call "aaa".length for example. How does that work?

  3. Functions are objects implementing [[Call]] according to this. What does that mean? (I'm thinking it's something about fun.call(this, arg1), but help me understand this.

    Also there is the typeof operator. I have linked it before from the MDN, but there are things confusing.

  4. typeof "aaa" === "string" and typeof String("aaa") === "string". This seems quite expectable, but what does String("aaa") return? I guess it parses the input somehow and returns a string primitive from it. Is this correct?

  5. typeof new String("aaa") === "object" What? Please explain its prototype chain to me. Where and on which prototype do I have the "aaa" primitive string value on this? How does it differ from typeof String("aaa")?

I have read and watched a lot of things on the subject and I think I need these clarified.

Also in your answers, if you link an external resource, please summarize its important part, and state what it means, because I have been reading through ecmascript specifications and they are quite long :).

Also if there is a difference in versions of javascript please state that too.

like image 339
vinczemarton Avatar asked Feb 02 '12 09:02

vinczemarton


People also ask

What is _proto_?

__proto__ is a way to inherit properties from an object in JavaScript. __proto__ a property of Object. prototype is an accessor property that exposes the [[Prototype]] of the object through which it is accessed. POSTly is a web-based API tool that allows for fast testing of your APIs (REST, GraphQL).

Is __ proto __ deprecated?

__proto__ Deprecated: This feature is no longer recommended.

What is Typeof array in JavaScript?

The typeof keyword is used to differentiate primitive types in JavaScript. It will return one of nine strings: undefined , object (meaning null), boolean , number , bigint , string , symbol , function , or object (meaning any object, including arrays).

How do you access object prototype?

Note: The property of an object that points to its prototype is not called prototype . Its name is not standard, but in practice all browsers use __proto__ . The standard way to access an object's prototype is the Object. getPrototypeOf() method.


2 Answers

1\ In my "interpretation of javascript" that means everything has the "object" in the beginning of it's prototype chain. Is this correct?

Yes and no. All objects, by default, inherit from Object. It is possible, using ES5's Object.create, to have an object that doesn't inherit from Object, but it's still an object.

2\ But what about the primitive types (string, number, boolean, null, undefined)? Are they objects? I can call "aaa".length for example. How does that work?

It's a misconception that everything is an object in JavaScript. Primitives aren't objects, but they can be converted to objects. When the . operator is used, the left operand is converted to an object (if possible).

3\ Functions are objects implementing [[Call]] according to this. What does that mean? (I'm thinking it's something about fun.call(this, arg1), but help me understand this.

[[Call]] is an internal method used by the ECMAScript implementation to mark objects as functions. It's not directly related to Function.prototype.call, which itself is also a function marked with [[Call]]. See 13.2.1 [[Call]].

4\ typeof "aaa" === "string" and typeof String("aaa") === "string". This seems quite expectable, but what does String("aaa") return? I guess it parses the input somehow and returns a string primitive from it. Is this correct?

String(), when not used as a constructor, converts its argument to a string primitive. So String("aaa") is the same as "aaa".toString(). It's redundant and unnecessary in this case.

5\ typeof new String("aaa") === "object" What? Please explain it's prototype chain to me. Where and on which prototype do I have the "aaa" primitive string value on this? How does it differ from typeof String("aaa")?

String() used as a constructor returns an object that inherits from String(), as you would expect. There's a difference between a string primitive and a string object.

Almost all of your questions can be answered by reading the specification whenever you're confused about something. For your convenience, there's an annotated version of the specification available online.

like image 114
Andy E Avatar answered Oct 20 '22 00:10

Andy E


1\ In my "interpretation of javascript" that means everything has the "object" in the beginning of it's prototype chain. Is this correct?

Ans: No, there are also primitive types as you said in question 2.

2\ But what about the primitive types (string, number, boolean, null, undefined)? Are they objects? I can call "aaa".length for example. How does that work?

Ans: No, they are primitive types, not objects. When you call "aaa".length, JavaScript will automatically wrap the string primitive to String object and call the method or perform the property lookup.

3\ Functions are objects implementing [[Call]] according to this. What does that mean? (I'm thinking it's something about fun.call(this, arg1), but help me understand this.

Ans:Every function in JavaScript is actually a Function object.

4\ typeof "aaa" === "string" and typeof String("aaa") === "string". This seems quite expectable, but what does String("aaa") return? I guess it parses the input somehow and returns a string primitive from it. Is this correct?

Ans: String("aaa") returns a primitive string.

5\ typeof new String("aaa") === "object" What? Please explain it's prototype chain to me. Where and on which prototype do I have the "aaa" primitive string value on this? How does it differ from typeof String("aaa")?

Ans: new String("aaa") returns a String object.

like image 25
xdazz Avatar answered Oct 20 '22 00:10

xdazz