472,952 Members | 1,870 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Would this work? combining two evil functions...

I have just discovered the "with" statement, which up until now I have
only known as "that which should never be used". I would like to
evaluate some commands (such as function definitions and the like)
within the global context, would this accomplish the task? Are there
better ways to deal with this? (Note that I am making dynamic
execution a requirement)

function executeAsGlobal(cmd)
{
with(window) exec(cmd);
}

Aug 30 '06 #1
7 1505
od*********@gmail.com said the following on 8/29/2006 9:58 PM:
I have just discovered the "with" statement, which up until now I have
only known as "that which should never be used".
That's not a true statement though. "with" can be used, and has it's
benefits.
I would like to evaluate some commands (such as function definitions
and the like) within the global context, would this accomplish the task?
Perhaps.
Are there better ways to deal with this? (Note that I am making dynamic
execution a requirement)
window[cmd]();

Not 100% sure if that gets it back into the global context though as it
is being called from within a function.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Aug 30 '06 #2
Randy Webb wrote:
od*********@gmail.com said the following on 8/29/2006 9:58 PM:
I have just discovered the "with" statement, which up until now I have
only known as "that which should never be used".

That's not a true statement though. "with" can be used, and has it's
benefits.
I would like to evaluate some commands (such as function definitions
and the like) within the global context, would this accomplish the task?

Perhaps.
Are there better ways to deal with this? (Note that I am making dynamic
execution a requirement)

window[cmd]();

Not 100% sure if that gets it back into the global context though as it
is being called from within a function.
Then:

var _global = this;

function testCommands()
{
/* evaluate _global[cmd] */
}

should remove all doubt and cover environments that don't have a window
object.
-
Rob

Aug 30 '06 #3
RobG said the following on 8/29/2006 10:53 PM:
Randy Webb wrote:
>od*********@gmail.com said the following on 8/29/2006 9:58 PM:
>>I have just discovered the "with" statement, which up until now I have
only known as "that which should never be used".
That's not a true statement though. "with" can be used, and has it's
benefits.
>>I would like to evaluate some commands (such as function definitions
and the like) within the global context, would this accomplish the task?
Perhaps.
>>Are there better ways to deal with this? (Note that I am making dynamic
execution a requirement)
window[cmd]();

Not 100% sure if that gets it back into the global context though as it
is being called from within a function.

Then:

var _global = this;

function testCommands()
{
/* evaluate _global[cmd] */
}
should remove all doubt
Does it? Context is key and without context of what the OP is doing, you
can't answer my question :)
and cover environments that don't have a window object.
If you are going to cover environments that don't have a window object,
then you also have to consider any potential environments that may not
have a global context.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Aug 30 '06 #4
Randy Webb wrote:
RobG said the following on 8/29/2006 10:53 PM:
Randy Webb wrote:
od*********@gmail.com said the following on 8/29/2006 9:58 PM:
[...]
>Are there better ways to deal with this? (Note that I am making dynamic
execution a requirement)
window[cmd]();

Not 100% sure if that gets it back into the global context though as it
is being called from within a function.
Then:

var _global = this;

function testCommands()
{
/* evaluate _global[cmd] */
}
should remove all doubt

Does it? Context is key and without context of what the OP is doing, you
can't answer my question :)
Are you referring to "as per the specification" or "in reality"?

According to the ECMAScript Language Specification, section 10.2, when
used in a global context, the this operator refers to the global object
- it is quite explicit about that. Therefore _global *must* refer to
the global object and _global[propertyName] must be interpreted as a
reference to a property of the global object. I'm keen to know if I've
got that wrong.

It therefore follows that if _global does not refer to the global
object, then the implementation does not conform to the spec. I don't
know of any such implementation, but I'm certainly no authority on
that. :-)

and cover environments that don't have a window object.

If you are going to cover environments that don't have a window object,
then you also have to consider any potential environments that may not
have a global context.
That was a throwaway line, I expect 99.99% of all ECMA-based script
appearing in this group is intended to only ever run in a browser - the
odd LiveScript fossil or ActionScript reactionary being exceptions
rather than the rule. I expected a few Mac OS X Dashboard widget
questions, but they are few and far between - maybe they get better
satisfaction in Apple's ADC forum. ;-).
--
Rob

Aug 30 '06 #5
RobG said the following on 8/30/2006 12:46 AM:
Randy Webb wrote:
>RobG said the following on 8/29/2006 10:53 PM:
>>Randy Webb wrote:
od*********@gmail.com said the following on 8/29/2006 9:58 PM:
[...]
>>>>Are there better ways to deal with this? (Note that I am making dynamic
execution a requirement)
window[cmd]();

Not 100% sure if that gets it back into the global context though as it
is being called from within a function.
Then:

var _global = this;

function testCommands()
{
/* evaluate _global[cmd] */
}
should remove all doubt
Does it? Context is key and without context of what the OP is doing, you
can't answer my question :)

Are you referring to "as per the specification" or "in reality"?

I think you already know the answer to that based on my opinions of the
specifications that I have expressed in the past. But just in case, to
answer the question:

"in reality" - without a doubt.

No implementation follows the specification 100% exactly and there may -
or may not - be one that follows the global aspect of the specs.

>>and cover environments that don't have a window object.
If you are going to cover environments that don't have a window object,
then you also have to consider any potential environments that may not
have a global context.

That was a throwaway line, I expect 99.99% of all ECMA-based script
appearing in this group is intended to only ever run in a browser
If it is that low of a percentage :)
- the odd LiveScript fossil or ActionScript reactionary being exceptions
rather than the rule.
True. And most of those get referred to other groups that deal with them
exclusively.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Aug 30 '06 #6
od*********@gmail.com writes:
I have just discovered the "with" statement, which up until now I have
only known as "that which should never be used".
Closer to "use with *extreme* care", because you have no way of knowing
what properties exist on the object you are "with"'ing with.
I would like to evaluate some commands (such as function definitions
and the like) within the global context, would this accomplish the
task? Are there better ways to deal with this? (Note that I am
making dynamic execution a requirement)

function executeAsGlobal(cmd)
{
with(window) exec(cmd);
No.

What "with" does is to add its operant to the scope chain before executing
the statements.
That means that the statement "exec(cmd)" is executed with variables
being resolved against the global object first, and the scope chain
of the "executeAsGlobal" closure afterwards. That is all.
It does not change the value of the "this" operator, or of the variables
object. That means that variables created by "exec(cmd)" will end up
in the variables objec of the invocation of "executeAsGlobal", not in
the global scope.
This might be sufficient for your use, but it is not the same as
executing within the global context.

Also, the command given here as example, "exec(cmd)" is a simple function
call. Both "exec" and "cmd" are looked up with the global object first
(potentially overriding the function parameter "cmd" if the global object
contains a "cmd" property), but the execution of the function "exec" will
happen in the scope chain stored in that function's closure.

If you mean "eval" instead of "exec", then the "cmd" code is executed
in the same scope as the call, so it inherits the enhanced scope chain
and the unchanged variables object. I.e.

function foo() {
with (window) { eval("var x = 42;"); }
}
foo();

would not create "x" as a global variable.

I have used this trick myself, but one has to be aware of the limitations.

(The only way I know of to have code executed in the global scope from
inside a function scope is to use setTimeout/setInterval with a string
parameter, but you can't do that synchroneously).

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Aug 30 '06 #7
Lasse Reichstein Nielsen wrote:
If you mean "eval" instead of "exec", then the "cmd" code is executed
in the same scope as the call, so it inherits the enhanced scope chain
and the unchanged variables object. I.e.

function foo() {
with (window) { eval("var x = 42;"); }
}
foo();

would not create "x" as a global variable.

I have used this trick myself, but one has to be aware of the limitations.
Figures. I've been doing some regexp stuff lately and seem to confuse
exec and eval way too much...

Yah, currently our logic is retrieving javascript function definitions
(and calls) as part of the dynamic page operation. Considering that
"function xyz(abc){}" dumps itself into the current variable scope, was
hoping for a way to be able to change that scope. Currently we're
using "xyz = function(abc) {}" without first declaring xyz, which
appears to work but gives jslint fits. Was hoping for a cleaner method.

Aug 30 '06 #8

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

Similar topics

7
by: Paddy McCarthy | last post by:
Hi, I am trying to use eval as little as possible but solve this problem. #If given:two or more lambda equations x=lambda : A < B y=lambda : C+6 >= 7 .... How do I create another lambda...
3
by: EviL KerneL | last post by:
Hi - I am working on a survey project which has a next button that takes you to the next set of questions and so on. It does this by means of an OnClick event which takes the answers from the...
2
by: Chris Mullins | last post by:
I've spent a bit of time over the last year trying to implement RFC 3454 (Preparation of Internationalized Strings, aka 'StringPrep'). This RFC is also a dependency for RFC 3491...
2
by: BrianP | last post by:
Hi, I have had to invent a work-around to get past what looks like a JavaScript bug, the malfunctioning Perl-like JavaScript array functions including SPLICE() and UNSHIFT(). I have boiled it...
22
by: Martin C | last post by:
I have inherited a database which has a table for each of our 1200 customers. Each table is exactly the same. It is very difficult to generate reports I would like to create a new table and take...
6
by: MLH | last post by:
SELECT Adresses.RawData FROM Adresses WHERE (((Adresses.RawData)=InStr(1,,Chr$(13)))); Its not doing the trick for me and I'm not sure why.
1
by: Swami Tota Ram Shankar | last post by:
"Bob Weigel" <dontuwish@nothing.net> wrote in message > > While George Bush, is the epitome of evil, racism, and uncompassionate > > conservatism, Kerry is either deluded, very deluded, or...
11
by: Alan | last post by:
Is there an easy and efficient way to combine two <vector>s, rather than taking each element from one and adding it to the other? I haven`t been able to find any guidance on or examples of this...
1
by: yuleball | last post by:
hi I am designing an editor program. I am using ChrW() and AscW() functions to print the character on screen. I want to implement combining diacritical marks(or accent marks) i-e if i print a...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...

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.