62593

Does String inherit from Object in Javascript?

Is <strong>Object</strong> 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, <strong>s1</strong> and <strong>s2</strong> are of diffeent type?

Answer1:

Yes, 'str' is a string literal, not a string object.

A string literal has access to all of a string's objects and methods because javascript will temporarily cast a string literal as a string object in order to run the desired method.

Finally:

Where the two differ is their treatment of new properties and methods. Like all Javascript Objects you can assign properties and methods to any String object. You can not add properties or methods to a string literal. They are ignored by the interpreter.

Read up more here.

Answer2:

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

new Object([primitive])

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

[boxedobject].valueOf()

to get the primitive.

In ECMAScript (javascript) the constructor of Object is able to box all primitives.

Answer3:

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

Recommend

  • regex - wrap all integers in double quotes
  • GetInterfaces() returns generic interface type with FullName = null
  • An unhandled exception of type 'System.AccessViolationException' occurred
  • Creating a Messenger service
  • Type definition: expected UnionAll, got TypeVar
  • Getting Started with LightInject
  • UWP navigation (Template10), Pivot control, multiple frames
  • Error binding to target method
  • Will these ActiveXObject and XMLHttpRequest checks apply for any other browser than IE6?
  • What's the benefit of the trailing apostrophe in character literals
  • Why “propdp” code snippet doesn't use the nameof operator for the name of the registered proper
  • How to map childs/parent class with petapoco?
  • Data Type of Columns in a List - R
  • SQL Server Nvarchar and Java prepared statement
  • Sort by a column in a union query in SqlAlchemy SQLite
  • How to write string.Contains(someText) in expression Tree
  • DependencyObject.AssociatedObject is always null
  • when does setTimeout start executing in a inline
  • How do I register classes by both interface and namespace with Windsor?
  • SAXReader not re-ecape characters
  • Deserializing XML into class C#
  • Weird JavaScript statement, what does it mean?
  • Function pointer “assignment from incompatible pointer type” only when using vararg ellipsis
  • Apache 2.4 - remove | delete | uninstall
  • Akka Routing: Reply's send to router ends up as dead letters
  • AT Commands to Send SMS not working in Windows 8.1
  • VB.net deserialize, JSON Conversion from type 'Dictionary(Of String,Object)' to type '
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • Unit Testing MVC Web Application in Visual Studio and Problem with QTAgent
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • Benchmarking RAM performance - UWP and C#
  • How can I get HTML syntax highlighting in my editor for CakePHP?
  • How do I configure my settings file to work with unit tests?
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • apache spark aggregate function using min value
  • python draw pie shapes with colour filled
  • Is it possible to post an object from jquery to bottle.py?
  • Sorting a 2D array using the second column C++
  • Binding checkboxes to object values in AngularJs
  • How to Embed XSL into XML