Is Object the base class of all objects in Javascript, just like other language such as Java & C#?
I tried below code in Firefox with Firebug installed.
var t = new Object();
var s1 = new String('str');
var s2 = 'str';
console.log(typeof t);
console.log(typeof s1);
console.log(typeof s2);
The console output is
object
object
string
So, s1 and s2 are of diffeent type?
The process is called boxing/unboxing.
This means that whenever the interpreter/compiler sees a primitive type used as an Object then it will use
to get a valid instance. And in the same way, as soon as you try to use it as a primitive (as in an expression) it will use
to get the primitive.
In ECMAScript (javascript) the constructor of Object is able to box all primitives.
Read this: http://skypoetsworld.blogspot.com/2007/11/javascript-string-primitive-or-object.html
and this: https://developer.mozilla.org/en/JavaScript/Glossary#primitive
and this: https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/String
Yes, 'str' is a string
literal
, not a stringobject
.Finally:
Read up more here.