473,399 Members | 2,278 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,399 software developers and data experts.

priveledged stranger?

I have a situation, where I'd like to dynamically create a function in
the global space (window.funcWillGoHere) and have it call a method (or
private function) of an instantiated object that it will not know the
name of.

I came up with this:

dude = function() {
function dudely() {
alert('dudely');
};
eval('window.test=dudely');
};
$dude = new dude();
window.test();
window.test is a privileged method, except it is a method of another object.

The question is; Is this reliable? Can I expect it will work in the future?

It seems to work in current browsers, but I just want to make sure it
isn't some quirk that will stop working at some point in the future.

Thanks,

Kevin N.

Jul 27 '05 #1
6 1233
Kevin Newman wrote:
I have a situation, where I'd like to dynamically create a
function in the global space (window.funcWillGoHere) and
have it call a method (or private function) of an
instantiated object that it will not know the name of.

I came up with this:

dude = function() {
function dudely() {
alert('dudely');
};
eval('window.test=dudely');
The eval call is redundant:-

window.test = dudely;

- will work exactly the same, and so just as well. Generally, if an eval
call contains a string literal then it can be replaced with the code
equivalent of that string.

};
$dude = new dude();
window.test();
window.test is a privileged method, except it is a method
of another object.

The question is; Is this reliable? Can I expect it will
work in the future?

<snip>

You can expect it to work with all ECMA 262 3rd edition implementations
operating in web browser environments (because the - window - identifier
is not part of javascript but is instead provided by the browser
environment).

Richard.
Jul 27 '05 #2
VK
Kevin Newman wrote:
I have a situation, where I'd like to dynamically create a function in
the global space (window.funcWillGoHere) and have it call a method (or
private function) of an instantiated object that it will not know the
name of.

Unless I'm missing some specifics of your situation, you should use the
proper chain construction via "call" or "apply" methods:

function dude() {
// Independent constructor: knows nothing
// about you "privileged stranger"
this.dudeMethod = function() {alert('Method of \'dude\'');}
}

function test() {
// priveledged stranger: get its own stuff first:
this.myMethod = function() {alert('Method of \'test\'');}
// then grabs the stuff from poor dude:
dude.call(this);
// if you don't need all dude stuff,
// look for apply(this,arguments) method
}

window.oInstance = new test();

window.oInstance.myMethod(); // Method of 'test'

window.oInstance.dudeMethod(); // Method of 'dude'

Jul 27 '05 #3
The eval call is redundant:-
The eval was put in because I will not know the name of the function
that "dudely" represents at runtime. I see now that the eval actually
wasn't needed for the question I asked..

In case you are interested, I will be generating the function to be
called from a third party (flash player) and I have no control over what
the name of the function it will call is, but I want that function to
then call a method of the object that created it.

You can expect it to work with all ECMA 262 3rd edition implementations
operating in web browser environments.


Sweet, that's exactly what I was looking for. Thanks :-)
Jul 28 '05 #4
This looks like some sort of Object inheritance (which is awesome!) but
isn't exactly what I'm looking for..

In my case, I will be generating the function that will be called from a
third party (flash player) and I have no control over what the name of
the function it will call is, but I want that function to then call a
method of the object that created it.
Jul 28 '05 #5
Kevin Newman wrote:
The eval call is redundant:-
The eval was put in because I will not know the name of
the function that "dudely" represents at runtime.


But does that make any difference?
I see now that the eval actually
wasn't needed for the question I asked..
And it probably still is not needed.
In case you are interested, I will be generating the function
to be called from a third party (flash player) and I have no
control over what the name of the function it will call is,
but I want that function to then call a method of the object
that created it.

<snip>

That doesn't seem particularly difficult, and certainly contains nothing
that requires an eval abuse.

Richard.
Jul 28 '05 #6
Richard Cornford wrote:
Kevin Newman wrote:
The eval call is redundant:-

The eval was put in because I will not know the name of
the function that "dudely" represents at runtime.


But does that make any difference?


I think you are right, I can probably use:

window[movieid+'_DoFSCommand'] = dudely;

If there is another way, I'd love to hear it :-)
Jul 29 '05 #7

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

Similar topics

11
by: blah | last post by:
Need python Pro at newtopython@googlegroups.com , if u wanna help, plz reply to that address. We are python beginners. need a real good Python Programmer for Help. TIA!! -- * Posted with...
7
by: Tamir Khason | last post by:
Very strange problem I have windows service, that using information from inside App.config by ConfigurationSettings.AppSettings <- Everything is ok here BUT, once I want to read string such as...
2
by: Martin Høst Normark | last post by:
Hi everyone Has anyone got the least experience in integrating the Digital Signature with an ASP.NET Web Application? Here in Denmark, as I supose in many other countries, they're promoting...
6
by: Kylin | last post by:
Public Class test1 Inherits System.Web.UI.Page #Region " Web Form Designer Generated Code " 'This call is required by the Web Form Designer. <System.Diagnostics.DebuggerStepThrough()> Private...
3
by: apandapion | last post by:
The following code snippet fails to compile with "a local variable named n is already defined in this scope." This makes me uncomfortable, but I could not tell you exactly why. My instincts are...
4
by: mark | last post by:
Ive created a aspx form using tables and flowlayout, I've set the background colour on certain rows, which show great in the browser, however when i use print preview the colours have...
0
by: Access Programming only with macros, no code | last post by:
ERROR MESSAGE: Could not update; currently locked by another session on this machine. BACKGROUND I have the following objects: Table1 - HO (which has about 51,000+ records) Table2 -...
2
by: LEX | last post by:
Could not update; currently locked by another session on this machine. Can someone help me figure out why access gives me this error on some record's memo fields but not on others?
1
by: Vivienne | last post by:
Hi there This is a hard problem that I have - I have only been using sql for a couple of weeks and have gone past my ability level quickly! The real tables are complex but I will post a simple...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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,...
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.