IF function === object === associatve array

R

relaxedrob

Howdy All!

If a function is an object and objects are associative arrays, are the
lines of code in a function held as members of the associative array?

Thanks for any advice!

Rob
:)
 
R

Richard Cornford

If a function is an object and objects are associative arrays, are the
lines of code in a function held as members of the associative array?

If they are that array is not ECMA Script specified or publicly exposed
by any implementations that I am aware of. (which means No!).

Richard.
 
R

Robert Bram

Hi Richard!

Perhaps I should have said hashtable. I was relying on information from
Douglas Crockford's site:

A Survey of the JavaScript Programming Language
http://www.crockford.com/javascript/survey.html

You can see that JavaScript's hashtable notation is very similar to
Java's object and array notations. JavaScript takes this much farther:
objects and hashtables are the same thing, so I could have written

var myHashtable = new Object();

and the result would have been exactly the same.


Am I still on the wrong track?

Rob
:)
 
D

Douglas Crockford

The relevant sentence from this page:
http://www.crockford.com/javascript/survey.html
is just this:

Objects are usually implemented as hash-tables.

Which made me wonder how lines of code are stored and referred to.

The lines of code are not usually visible properties of a function. The
ECMAScript standard does not provide for retrieving the source of a function,
although some implementations provide .toSource or .toString methods which
provide access in a non-standard way.

There is performance evidence to suggest that objects in JScript are implemented
not as hashtables, but as lists of some sort. But the interfaces are the same as
in JavaScript.
 
R

Richard Cornford

Robert Bram said:
I should correct myself here.

The relevant sentence from this page:
http://www.crockford.com/javascript/survey.html
is just this:

Objects are usually implemented as hash-tables.

Which made me wonder how lines of code are stored and referred to.

And the answer is internally and in an implementation dependent way.
Stop worrying about lines of source code; the most you will get is the
result of type-converting a function to a string (usually the source
code, but again that is up to the implementation and web browsers are
not consistent). (If implemented, type conversion to a string will
involve the calling of the function's - toString - method.)

Richard.
 
L

Lasse Reichstein Nielsen

Robert Bram said:
Objects are usually implemented as hash-tables.

Which made me wonder how lines of code are stored and referred to.

They aren't. The source code of a function is kept internally ...
somehow, and is only emitted if you call the function's toString
method [1]. There is no direct access to the code.

/L
[1] I can see that Opera keeps the code as a parsed syntax tree, and
unparses it to turn it into a string. The current beta makes at least
one errors in the output, so I can see that it doesn't remember the
original code verbatim.
 

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
474,085
Messages
2,570,597
Members
47,219
Latest member
Geraldine7

Latest Threads

Top