Connecting Tech Pros Worldwide Forums | Help | Site Map

priveledged stranger?

Kevin Newman
Guest
 
Posts: n/a
#1: Jul 27 '05
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.


Richard Cornford
Guest
 
Posts: n/a
#2: Jul 28 '05

re: priveledged stranger?


Kevin Newman wrote:[color=blue]
> 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');[/color]

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.

[color=blue]
> };
> $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?[/color]
<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.


VK
Guest
 
Posts: n/a
#3: Jul 28 '05

re: priveledged stranger?


Kevin Newman wrote:[color=blue]
> 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.[/color]


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'

Kevin Newman
Guest
 
Posts: n/a
#4: Jul 28 '05

re: priveledged stranger?


[color=blue]
> The eval call is redundant:-[/color]

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.

[color=blue]
> You can expect it to work with all ECMA 262 3rd edition implementations
> operating in web browser environments.[/color]

Sweet, that's exactly what I was looking for. Thanks :-)


Kevin Newman
Guest
 
Posts: n/a
#5: Jul 28 '05

re: priveledged stranger?


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.
Richard Cornford
Guest
 
Posts: n/a
#6: Jul 29 '05

re: priveledged stranger?


Kevin Newman wrote:[color=blue][color=green]
>> The eval call is redundant:-[/color]
>
> The eval was put in because I will not know the name of
> the function that "dudely" represents at runtime.[/color]

But does that make any difference?
[color=blue]
> I see now that the eval actually
> wasn't needed for the question I asked..[/color]

And it probably still is not needed.
[color=blue]
> 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.[/color]
<snip>

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

Richard.


Kevin Newman
Guest
 
Posts: n/a
#7: Jul 29 '05

re: priveledged stranger?


Richard Cornford wrote:[color=blue]
> Kevin Newman wrote:[color=green][color=darkred]
>>> The eval call is redundant:-[/color]
>> The eval was put in because I will not know the name of
>> the function that "dudely" represents at runtime.[/color]
>
> But does that make any difference?
>[/color]

I think you are right, I can probably use:

window[movieid+'_DoFSCommand'] = dudely;

If there is another way, I'd love to hear it :-)
Closed Thread