priveledged stranger?

K

Kevin Newman

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.
 
R

Richard Cornford

Kevin said:
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.
 
V

VK

Kevin said:
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'
 
K

Kevin Newman

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 :)
 
K

Kevin Newman

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.
 
R

Richard Cornford

Kevin said:
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.
 
K

Kevin Newman

Richard said:
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 :)
 

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
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top