+ 2

primitive data types and wrapper objects(type conversion)

Is what I'm saying correct? string, number, object, function, symbol,etc, are primitive data types.(objects are String Number, Date, Object,etc)when we use them as object they convert to object automatically example: (2.9).toFixed()//this number convert to Number object. There is an exception here that function are actually objects. try this : function f(a) { return a; } var b=new f(function () {}) console.log (typeof b) // function result is function. while if we pass an non-function type to this object-constructor, result is object.What other reason could it have been other than that the functions are callable objects?

14th Apr 2021, 7:16 PM
Mehran
Mehran - avatar
4 Answers
+ 6
You got some of the primitives correct but function and object incorrect. Here's the full list of 7 with their corresponding wrapper objects in brackets: string (String) number (Number) boolean (Boolean) symbol (Symbol) bigint (BigInt) undefined null Primitives don't have methods. You're right to say they get converted to the corresponding wrapper objects to allow you to call methods. One thing to note is that once you're finished with the wrapper object, it's removed. So, you're left with a primitive again. So, we've touched on primitive types. The other major category is object types (also called reference types). There are 3: Object, Array, and Function. So yes, functions are objects. More specifically, they are callable objects as you correctly say. They are also first-class objects, which means they can have methods and properties and methods just like any other object. Examples showing the length property and toString() method of Function objects: const f = (x) => {} console.log(f.length) // 1 const g = () => {} console.log(g.toString()) // "() => {}"
14th Apr 2021, 9:02 PM
CamelBeatsSnake
CamelBeatsSnake - avatar
+ 2
CamelBeatsSnake ok, I ignored some primitive types like bigint
14th Apr 2021, 10:14 PM
Mehran
Mehran - avatar
+ 1
there is very interesting answer
23rd Jun 2021, 12:41 PM
What Is The Meaning Of The Life
What Is The Meaning Of The Life - avatar
+ 1
Thanks for the information you shared. Loved the way you explained everything in this blog. http://www.indigocard.vip/
24th Nov 2021, 10:10 AM
Leonard Stephens