is the object literal form the same as an array?

J

Jake Barnes

I was just reading this article on Ajaxian:

http://ajaxian.com/archives/show-love-to-the-object-literal

This is a newbie question, but what is the object literal? I thought it
was like an array notation, but is it really its own thing? You can
declare arrays this way, can't you? An object in Javascript is like a
hash array that stores pointers to properties and methods?

Or is object literal notation a convenience form, wholly arbitrary,
unrelated to anything else in the language, existing for the ease of
the programmer?
 
V

VK

Jake said:
I was just reading this article on Ajaxian:

http://ajaxian.com/archives/show-love-to-the-object-literal

This is a newbie question, but what is the object literal? I thought it
was like an array notation, but is it really its own thing? You can
declare arrays this way, can't you? An object in Javascript is like a
hash array that stores pointers to properties and methods?

Or is object literal notation a convenience form, wholly arbitrary,
unrelated to anything else in the language, existing for the ease of
the programmer?

See: <http://www.geocities.com/schools_ring/ArrayAndHash.html>

1) var myObject1 = new Object();
2) var myObject2 = {};

gives you the same outcome, but the second variant is currently
considered to be cool, and the first one is not :) As everyone wants
to be cool on public, the 2nd one is what you'll see most of the time.
:)

1) var myArray1 = new Array();
2) var myArray2 = [];

same outcome with the same reasons behind. The only situation may be if
you want to create an array and declare its length right away (but do
not populate it). In such case 1) is the choice: new Array(100).
 
T

Thomas 'PointedEars' Lahn

Jake said:
I was just reading this article on Ajaxian:

http://ajaxian.com/archives/show-love-to-the-object-literal

This is a newbie question, but what is the object literal?

(Ignore VK when it comes to arrays; be very careful with his
statements when it comes to the rest of programming.)

The Object literal, to be exact, is an Object object initializer.
I thought it was like an array notation,

The syntax of Object literals is similar but not equal to that of Array
literals. Object literals can define properties with all valid names;
Array literals can define only properties with numerical name which serve
as array elements later.
but is it really its own thing?

It is. The objects created by Object literals inherit directly from
the core Object object, not from the core Array object. They inherit
indirectly from the Object object, though, through the prototype chain:
The internal [[Prototype]] property of Array objects is/refers to
Array.prototype of course, and Array.prototype.[[Prototype]] is/refers
to Object.prototype.
You can declare arrays this way, can't you?

You cannot. The resulting object lacks essential features of Array objects,
which not only include the methods of Array.prototype, such as join().
An object in Javascript is like a hash array that stores pointers to
properties and methods?

No. While the term "hash array" and its mere applicability to ECMAScript
Object objects is heavily debated (and refused at least by me), there are
no pointers in ECMAScript implementations at all. Properties of objects
may be object references, but they do not have to; they may store primitive
values as well. Methods are properties that store references to Function
objects.
Or is object literal notation a convenience form,

It is. Instead of

var o = new Object();
o.foo = 'bar';

aso. you can write

var o = {foo: 'bar', ...};

As you can write

var a = [42, , 23];

instead of

var a = new Array();
a[0] = 42;
a[2] = 23;
wholly arbitrary,

No, they follow syntax rules. For example, in {name: value},
`name' must be an identifier, a number or a string literal;
`value' may be any expression.
unrelated to anything else in the language,
No.

existing for the ease of the programmer?

Most certainly.


PointedEars
 
R

RobG

Jake said:
I was just reading this article on Ajaxian:

http://ajaxian.com/archives/show-love-to-the-object-literal

This is a newbie question, but what is the object literal? I thought it
was like an array notation, but is it really its own thing? You can
declare arrays this way, can't you? An object in Javascript is like a
hash array that stores pointers to properties and methods?

Or is object literal notation a convenience form, wholly arbitrary,
unrelated to anything else in the language, existing for the ease of
the programmer?

I guess when you ask such a question you are going to get lots of
different opinions in reply, so here's mine. Thomas has given you most
of it, I think there are two may issues: attempting to 'type' variables
is pointless, and literals are convenient.

Attempting to 'type' a variable by initialising it with a dummy value
generally doesn't make sense[1] because JavaScript variables don't have
a type, it is their value that has a type, e.g.

var x = new Object();

Leads to the idea that 'x is an Object', but strictly x is a reference
to an Object (in this case, an empty object). The above statement does
not 'type' x as an Object; it doesn't force the value referenced by x to
always be an object.

e.g.

var x = new Object();
x = 5;

The value of x is changed from an object to a number.


A literal is a convenient way to initialise the value of a variable and
can be used with any type, Thomas' examples cover that.

Some confusion might arise from:

var x = 'hello';

not being the same as:

var y = new String('Hello');

'x' is a string primitive, whereas 'y' is a string object. But 'x' will
be converted to a string object if required and possible[2], e.g. I can
still do:

alert( x.length ); // Shows 5
alert( x.split('') ); // Shows h,e,l,l,o

The value of x is still a primitive, it is converted only for the sake
of evaluating the expression.

Extensive use of literals leads to JavaScript Object Notation (JSON):

<URL:http://www.json.org/>


1. Typing of variables has been proposed as part of Netscape's
JavaScript 2.0 and the Netscape proposal for ECMAScript Ed 4.
<URL:http://www.mozilla.org/js/language/js20/index.html>

JScript .NET (JScript 7) implements typing of variables:
<URL:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/jscript7/html/jsoridatatypes.asp
2. The ECMAScript Language Specification Ed 3, Section 9:

"The ECMAScript runtime system performs automatic type
conversion as needed."

Read the rest of the section to get an idea of how it works.
 
T

Thomas 'PointedEars' Lahn

Thomas said:
Jake said:
[Object literal vs. Array literal]
but is it really its own thing?

It is. The objects created by Object literals inherit directly from
the core Object object, not from the core Array object. They inherit
indirectly from the Object object, though, through the prototype chain:
The internal [[Prototype]] property of Array objects is/refers to
Array.prototype of course, and Array.prototype.[[Prototype]] is/refers
to Object.prototype.

Should have been:

[...] _Objects created by Array literals_ inherit indirectly from the
Object object, though, through the prototype chain: The internal
[[Prototype]] property of Array objects is/refers to Array.prototype of
course, and Array.prototype.[[Prototype]] is/refers to Object.prototype.

Sorry for causing confusion.


PointedEars
 
T

Thomas 'PointedEars' Lahn

RobG said:
[...]
var x = new Object();

Leads to the idea that 'x is an Object', but strictly x is a reference
to an Object (in this case, an empty object).

That is not quite correct. There are no empty objects in ECMAScript
implementations :)

The object referred to here is merely an object that has no enumerable
properties (however, it can have non-enumerable external properties such
as __proto__ in JavaScript). Among others, it also has an non-enumerable
internal [[Prototype]] property which allows it to inherit several
non-enumerable properties from Object.prototype [such as constructor,
toString(), valueOf()], and if Object.prototype was augmented even
enumerable ones, through the prototype chain.
The above statement does not 'type' x as an Object; it doesn't force the
value referenced by x to always be an object.

e.g.

var x = new Object();
x = 5;

The value of x is changed from an object to a number.
^^^^^^^^^
<nitpick> from an (object) reference to a number </nitpick>


Regards,
PointedEars
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,045
Latest member
DRCM

Latest Threads

Top