473,729 Members | 2,371 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1540
od*********@gma il.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.javas cript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Aug 30 '06 #2
Randy Webb wrote:
od*********@gma il.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*********@gma il.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.javas cript 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*********@gma il.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*********@gma il.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.javas cript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Aug 30 '06 #6
od*********@gma il.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 "executeAsGloba l" 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 "executeAsGloba l", 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/rasterTriangleD OM.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
4367
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 expression Z equivalent to
3
14231
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 questions and adds them to a database while taking you to the next page. I have a second onclick event I would like to combine with the above which changes the value of the button to "please wait" (to prevent the user from click on the buton more...
2
3275
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 (Internationalized Domain Names / IDNA) which is something that I also need to support. The problem that I've been struggling with in .NET is that of Unicode Code Points > 0xFFFF. These points are encoded into UTF8 using the Surrogate Pair encoding scheme that...
2
5232
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 down to a very simple test case which can be cut-n-pasted into a .html file and viewed in a browser: ============================================================================
22
698
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 in all the records of all the tables into this database. The new table will have an extra field which will hold the table name for the records imported from the tables. Can anyone help?
6
1678
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
2667
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 plainly a liar. > As to bush being racist...sorry I dont' see that. You make nasty > presumptions about others which makes you no fun to read... uncompassionate > conservativism...I'd like to take you on a trip where we help people. I'd > give...
11
10608
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 sort of operation. Thanks, Alan
1
1377
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 character and then i want to put diacritic on it by using specific key then i shud be able to place diacritic above or below that character so that they appear as one character. Is there any way to accomplish this in vb?
0
8921
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8763
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9202
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9148
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6022
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4528
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3238
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2683
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2165
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.