"eval" query

D

Dr J R Stockton

Is there any string S for which eval(S) will give a Date Object? If
so, for the pedants, what is it?

If there is, existing code will serve with added text. Otherwise, new
code will need to be written.
 
E

Eric Bednarz

Dr J R Stockton said:
Is there any string S for which eval(S) will give a Date Object? If
so, for the pedants, what is it?

I really wonder what you intended to ask. As far as I am concerned,

'new Date()'

is a string and

eval('new Date()')

does the obvious.
 
D

Dr J R Stockton

I really wonder what you intended to ask. As far as I am concerned,

  'new Date()'

is a string and

  eval('new Date()')

does the obvious.


But what do you think is obvious? Did you test it? Would you expect
to get a function, another type of Object, or a string? Would you be
right?


I intended to ask what I asked; and that is not what you have
answered. Your answer only tells us that you think you are right.


The page in question being open at the time, S = "new Date()" is
exactly what I tried first - because it was the quickest first move,
quicker than opening or trying to remember the code. Only on reading
the code did I *know* that it used eval(S) .

If I put S = "Math" , I get an Object, with properties such as sin &
cos which Safari can list. If I put S = "Date" , I get a function,
with properties such as UTC & now which Safari can list.

But I want to get an Object with value an IEEE Double of integer value
and properties such as setDate & toJSON which Safari can list, and I
want to do it by entering a string S, containing "new Date" and
suitable other characters. It may not be possible.


Also, what is the proper terminology to sustinguish between objects
like the Math object and a Date object?
 
L

Lasse Reichstein Nielsen

Dr J R Stockton said:
But what do you think is obvious?

That
eval('new Date()')
evaluates to a Date object?
Did you test it?

No. That's what the specification of eval says should happen. I don't
test things for doing what they are supposed to do without a reason
to suspect that thay don't. Do you have any reason to suppose it won't
give a Date object?
Would you expect to get a function, another type of Object, or a string?
No.

Would you be right?

No if I expected something not a Date object, no.

I intended to ask what I asked; and that is not what you have
answered. Your answer only tells us that you think you are right.

And yet, that answer contained an answer to your question. It have a
string such that eval of that string gives a Date object.
The page in question being open at the time, S = "new Date()" is
exactly what I tried first - because it was the quickest first move,
quicker than opening or trying to remember the code. Only on reading
the code did I *know* that it used eval(S) .

If I put S = "Math" , I get an Object, with properties such as sin &
cos which Safari can list. If I put S = "Date" , I get a function,
with properties such as UTC & now which Safari can list.

As expected.
But I want to get an Object with value an IEEE Double of integer value
and properties such as setDate & toJSON which Safari can list, and I
want to do it by entering a string S, containing "new Date" and
suitable other characters. It may not be possible.

You haven't yet said what you did get when you tried. I.e.,
What did you do? What happened? What did you expect to happen?

So, now I tested it. If I execute:
var S = "new Date";
alert(Object.prototype.toString.call(eval(S)));
it alerts "[object Date]".
And if I execute:
var S = "new Date";
alert("setDate" in eval(S));
it alerts "true"

And I just checked it in Safari too. Same result.
If you get something else, you are doing something else.
Also, what is the proper terminology to sustinguish between objects
like the Math object and a Date object?

Are you trying to distinguish between the Date function (aka. Date
constructor), and a Date object? The latter is an object created
by using the Date constructor with the "new" operator. The former
is a function, the latter is not.

I see no reason to distinguish between the Math object and a Date
object, except for the detail that there is only one Math object
(hence the "the"), but using the Date constructor you can create
multiple Date objects.

/L
 
E

Eric Bednarz

The page in question

Is undisclosed, hence irrelevant.
But I want to get an Object with value an IEEE Double of integer value
and properties such as setDate & toJSON which Safari can list, and I
want to do it by entering a string S, containing "new Date" and
suitable other characters. It may not be possible.

osx:~ eric$ js
Rhino 1.7 release 3 2011 05 09
js> var evil = eval('new Date')
js> evil
Mon Sep 26 2011 02:04:59 GMT+0200 (CEST)
js> +evil
1316995499868
js> typeof evil
object
js> evil.constructor
function Date() { [native code for Date.Date, arity=1] }

js> evil.setDate
function setDate() { [native code for Date.setDate, arity=1] }

js> evil.setDate(1)
1314835499868
js> evil.toJSON
function toJSON() { [native code for Date.toJSON, arity=1] }

js> evil.toJSON()
2011-09-01T00:04:59.868Z
js>

etc.

I agree with Lasse; what did you do, what did you expect, what did you
get instead.

I would appreciate if that was supposed to be an em dash. :)
 
D

Dr J R Stockton

That
  eval('new Date()')
evaluates to a Date object?

I'm a little confused here - to reply "No" there you need to be Eric
Bednarz.
No. That's what the specification of eval says should happen. I
don't
test things for doing what they are supposed to do without a reason
to suspect that thay don't. Do you have any reason to suppose it
won't give a Date object?

I did. As I recall, and as I said in my previous reply, if I put in
"Math" I got results appropriate to the Math object, showing for
example that its .cos existed; but when I put in, or thought I put in,
"new Date()" I did not get an indication that its .getTime existed.

Are you trying to distinguish between the Date function (aka. Date
constructor), and a Date object? The latter is an object created
by using the Date constructor with the "new" operator. The former
is a function, the latter is not.

I see no reason to distinguish between the Math object and a Date
object, except for the detail that there is only one Math object
(hence the "the"), but using the Date constructor you can create
multiple Date objects.

The distinction is that, when JavaScript starts, a Date object exists,
with a property referenced by Date.UTC; AFAIK, that Date contains no
value, there is bo Date.valueOf. But, when JavaScript starts, there
are no objects such as are created ny new Date().

Earlier, my page shwed distinct difference in behavious between the
two cases, and therefore I needed a suitable nomenclature. Now that
both are broken, that's less important, at least if neither or both
get mended.

I see that the small Flanagan book ... fumble fumble ... and the big
one class .getDate under "Methods" and .UTC under "Static Methods",
which is not entirely satisfactory - I'd need to use "non-Static
Methods".


In my earlier testing independently of the page of interest, I must
have been doing something wrong.


To show why one should not believe that browsers always behave as the
standards imply, consider
new Date(Y, 0, 1) - new Date(Y + "/01/01")
One might expect a result of zero, always. But not in my Safari 5.1
(Win XP sp3), where y=2050 gives 864e5 (IIRC, about a third of years
do that). Then, for some of us, there's always the possibility of
having misunderstood the standard.


For further possible response, I'll wait till my proper newsreader
gets the articles.
 
J

Jukka K. Korpela

26.9.2011 22:11 said:
As I recall, and as I said in my previous reply, if I put in
"Math" I got results appropriate to the Math object, showing for
example that its .cos existed; but when I put in, or thought I put in,
"new Date()" I did not get an indication that its .getTime existed.

You could have made this discussion much shorter by telling what you
actually tested and why you think it failed.

For example, document.write(eval('new Date()').getTime()) appears to
write what one might expect, a large number.
 
D

Dr J R Stockton

Mon said:
You could have made this discussion much shorter by telling what you
actually tested and why you think it failed.

Only if I could remember what it actually was.

Veterans might have suspected it to have initially been the first form
in <http://www.merlyn.demon.co.uk/js-props.htm>, for which the code is
in <http://www.merlyn.demon.co.uk/inc-prop.js>.

The next thing is that entering the strings Math & Date give,
with the lower radio-button selected, entries for sections 2 to 6; but
with new Date() sections 2 to 6 do not appear with either button.
 
J

Jukka K. Korpela

27.9.2011 21:23 said:
Only if I could remember what it actually was.

I guess that's your way of apologizing the confusion you've caused.
Veterans might have suspected it to have initially been the first form
in <http://www.merlyn.demon.co.uk/js-props.htm>, for which the code is
in <http://www.merlyn.demon.co.uk/inc-prop.js>.

Are "veterans" supposed to be mind-readers and make sense of a
pointlessly messy form?
The next thing is that entering the strings Math & Date give,
with the lower radio-button selected, entries for sections 2 to 6; but
with new Date() sections 2 to 6 do not appear with either button.

This looks like a rather indirect way of saying that you called
Object.getOwnPropertyNames() with an argument that is a Date instance
and was surprised at seeing that it returned an empty array.

The issue does not seem to have anything to do with eval(). You can test
it directly by using e.g.
alert(Object.getOwnPropertyNames(new Date()).length);

Which own properties did you expect a Date instance to have? ECMA-262
does not mention any. It says:

"15.9.6 Properties of Date Instances
Date instances inherit properties from the Date prototype object and
their [[Class]] internal property value is "Date". Date instances also
have a [[PrimitiveValue]] internal property."

Internal properties are not returned by Object.getOwnPropertyNames().

Thus, eval('new Date()') returns a Date instance. Your method of testing
whether it does is just faulty.
 

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

Similar Threads


Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,906
Latest member
SkinfixSkintag

Latest Threads

Top