473,386 Members | 1,962 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

is the object literal form the same as an array?

I was just reading this article on Ajaxian:

http://ajaxian.com/archives/show-lov...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?

Feb 19 '06 #1
6 4790
VK

Jake Barnes wrote:
I was just reading this article on Ajaxian:

http://ajaxian.com/archives/show-lov...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).

Feb 19 '06 #2
VK wrote:
Jake Barnes wrote:
I was just reading this article on Ajaxian:

http://ajaxian.com/archives/show-lov...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>


No, don't. It is still utter nonsense, masked with buzzwords, despite
numerous attempts of competent people to explain this to the author (VK).
PointedEars
Feb 19 '06 #3
Jake Barnes wrote:
I was just reading this article on Ajaxian:

http://ajaxian.com/archives/show-lov...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
Feb 19 '06 #4
Jake Barnes wrote:
I was just reading this article on Ajaxian:

http://ajaxian.com/archives/show-lov...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/de...idatatypes.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.
--
Rob
Feb 19 '06 #5
Thomas 'PointedEars' Lahn wrote:
Jake Barnes wrote:
[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
Feb 20 '06 #6
RobG wrote:
[...]
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
Feb 20 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: sunaina | last post by:
This is the first program I am writing using PHP and Mysql. I am creating a game where user thinks of an object and my program guesses the object while asking series of yes/no questions. All a...
5
by: August1 | last post by:
This is a short program that I have written from a text that demonstrates a class object variable created on the stack memory and another class object variable created on the heap memory. By way...
4
by: jerrygarciuh | last post by:
Could someone please tell me the correct way to decalre this multidimensional array? TIA! jg var n = = = 25, = 0 ], = = 10, = 20 ],
54
by: tshad | last post by:
I have a function: function SalaryDisplay(me) { var salaryMinLabel = document.getElementById("SalaryMin"); salaryMinLabel.value = 200; alert("after setting salaryMinLabel = " +...
16
by: sneill | last post by:
How is it possible to take the value of a variable (in this case, MODE_CREATE, MODE_UPDATE, etc) and use that as an object property name? In the following example I want 'oIcon' object to have...
6
by: Luke | last post by:
Here is my emails to Danny Goodman (but probably he is very busy so he didn't answered it). First email(simple): Subject: JavaScript Arrays " We all know the array can act like HashMap, but is...
38
by: VK | last post by:
Hello, In my object I have getDirectory() method which returns 2-dimentional array (or an imitation of 2-dimentional array using two JavaScript objects with auto-handled length property - please...
7
by: Eric Laberge | last post by:
Aloha! This question is meant to be about C99 and unnamed compound objects. As I read, if such a construct as int *p = (int){0}; is used within a function, then it has "automatic storage...
4
by: rn5a | last post by:
Consider the following code: <script runat="server"> Sub Page_Load(ByVal obj As Object, ByVal ea As EventArgs) Dim dInfo As DirectoryInfo dInfo = New DirectoryInfo(Server.MapPath("/Folder1"))...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.