1

More specifically, how would you determine if a certain object was created using a literal or not?

var s1 = new String();
var s2 = ""; // Literal

var o1 = new Object();
var o2 = {}; // Literal

var f1 = new Function();
var f2 = function(){}; // Literal

Obviously if you compare any two above, for example:

var o1 = new Object();
var o2 = {};

alert(o1 == o2);
alert(o1 === o2);

alert(typeof o1);
alert(typeof o2);

... The first two alerts will show false while the last two alerts will give [Object object]

Say for example, if I wanted to do this:

function isLiteral(obj, type) {
    // ...
}

... how would one go about doing this?

I have taken a look at How to determine if an object is an object literal in Javascript?, but it does not answer my question.

Community
  • 1
  • 1
Shaz
  • 15,637
  • 3
  • 41
  • 59
  • 1
    Did you try this instead answer - http://stackoverflow.com/questions/332422/how-do-i-get-the-name-of-an-objects-type-in-javascript – Jason McCreary Apr 20 '11 at 23:22
  • 1
    You would also get false if `o1 = {}` and `o2 = {}`, or if `o1 = new Object()` and `o2 = new Object()`. Javascript checks for reference equality. Afaik there is no difference between an object and an object literal. – Elian Ebbing Apr 20 '11 at 23:24
  • @ElianEbbing: I rephrased my question, hope it helps. – Shaz Apr 20 '11 at 23:37
  • One last thing, there is no such thing as a "function literal". The RHS of `var f2 = function(){};` is a **function expression**, which is very similar to a function declaration. – RobG Apr 21 '11 at 00:43

1 Answers1

5

Firstly, the difference between these two lines:

var s1 = new String();
var s2 = ""; // Literal

...and the difference between these two lines:

var o1 = new Object();
var o2 = {}; // Literal

...are two different concepts.

The first is the difference between a primitive value and an object, while the second is... different syntax for the same thing.


Strings, numbers and booleans are primitive values, not objects, but can be wrapped as objects using new String(), new Number() or new Boolean(). So for these, typeof will return different values:

var s1 = new String();
typeof s1; // "object"
var s2 = "";
typeof s2; // "string"

However, for Object and Function, the difference between:

var o1 = new Object();
var o2 = {};

... is in syntax only.

Both o1 and o2 have the same prototype, and the same constructor, making them indistinguishable at runtime.

Šime Vidas
  • 182,163
  • 62
  • 281
  • 385
David Tang
  • 92,262
  • 30
  • 167
  • 149
  • This answer is missing a conclusion. Presumably, "sorry, but there is no distinction". – Lightness Races in Orbit Apr 20 '11 at 23:44
  • Say I had `var o2 = {}, o3 = new Object(o2);` Is there a way to tell if `o3` is indeed a new object of `o2`? – Shaz Apr 20 '11 at 23:44
  • @Shaz, nope, because `o2===o3`. – David Tang Apr 20 '11 at 23:50
  • @Shaz, to clarify, `o3` is *not* a new object of `o2` at all. This is the tricky side of `new`... it doesn't necessarily mean a new object is created. – David Tang Apr 21 '11 at 00:10
  • it may be better to explain that per ECMA-262, if o2 references an object, then `new Object(o2)` simply returns a reference to that object. Assigning the result to o3 means that o2 and o3 reference the same object. – RobG Apr 21 '11 at 00:41