Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Javascript (http://www.velocityreviews.com/forums/f68-javascript.html)
-   -   __parent__ property no longer works in firefox as expected (http://www.velocityreviews.com/forums/t938482-__parent__-property-no-longer-works-in-firefox-as-expected.html)

jgabios 04-21-2009 12:45 PM

__parent__ property no longer works in firefox as expected
 
i have firefox 3.0.8.
and i have the following code:

(function(){
function N(){
alert('N');
}
function Y(){
alert('Y');
}
window.goog=Y;
})();

if i do goog.__parent__ it gives me null;

the code:
function A(){
alert('A');
}

window.A.__parent__ gives me reference to the window.

is there another way to get a reference to the anonymous function
first listed as goog(it is Y).__parent__ goes to null?

thank you in advance.
--
jgabios
http://bash.editia.info

Thomas 'PointedEars' Lahn 04-21-2009 01:35 PM

Re: __parent__ property no longer works in firefox as expected
 
jgabios wrote:
> i have firefox 3.0.8.
> and i have the following code:
>
> (function(){
> function N(){
> alert('N');


window.alert(...);

`alert' is no longer a JavaScript feature since version 1.4, and it has
never been a language feature in other ECMAScript implementations.

> }
> function Y(){
> alert('Y');


See above.

> }
> window.goog=Y;


Don't augment host objects.

One refers to the ECMAScript Global Object explicitly by `this' if the
caller is the Global Object or null, or e.g. by `_global' in local context
with `_global = this' in global context:

(
function ...(...) {
this.goog = ...
}
)()

or

var _global = this;

function ...(...)
{
_global.goog = ...
}

Usually you would declare the identifier a variable and make use of the
scope chain, though, or you would use the function as the object that it is.

var goog;

function foo(...)
{
goog = ...

/* or */

foo.goog = ...
/* or */
arguments.callee.goog = ...
}

> })();
>
> if i do goog.__parent__ it gives me null;
>
> the code:
> function A(){
> alert('A');
> }
>
> window.A.__parent__ gives me reference to the window.


Consider that a happy coincidence. (Netscape/Mozilla.org) JavaScript is but
one of many ECMAScript implementations (see e.g. Microsoft JScript), and
`window' refers to a host object. (I have neither ever heard of that
property before nor did I need to make use of such, and I need two hands to
count the years I'm doing ECMAScript implementations now. But thanks for
mentioning, it will be added to the ECMAScript Support Matrix.)

> is there another way to get a reference to the anonymous function
> first listed as goog(it is Y).__parent__ goes to null?


IIUC,

(
function() {
function N(){
window.alert('N');
}

function Y() {
window.alert('Y');
}

return arguments.callee;
}
)()

However, one wonders what do you think you need this for, and why.


PointedEars

David Mark 04-21-2009 03:01 PM

Re: __parent__ property no longer works in firefox as expected
 
On Apr 21, 9:35*am, Thomas 'PointedEars' Lahn <PointedE...@web.de>
wrote:
> jgabios wrote:
> > i have firefox 3.0.8.
> > and i have the following code:

>
> > (function(){
> > function N(){
> > alert('N');

>
> * window.alert(...);
>
> `alert' is no longer a JavaScript feature since version 1.4, and it has
> never been a language feature in other ECMAScript implementations.
>
> > }
> > function Y(){
> > alert('Y');

>
> See above.
>
> > }
> > window.goog=Y;

>
> Don't augment host objects.
>
> One refers to the ECMAScript Global Object explicitly by `this' if the
> caller is the Global Object or null, or e.g. by `_global' in local context
> with `_global = this' in global context:
>
> * (
> * * function ...(...) {
> * * * this.goog = ...
> * * }
> * )()
>
> or
>
> * var _global = this;
>
> * function ...(...)
> * {
> * * _global.goog = ...
> * }
>
> Usually you would declare the identifier a variable and make use of the
> scope chain, though, or you would use the function as the object that it is.
>
> * var goog;
>
> * function foo(...)
> * {
> * * goog = ...
>
> * * /* or */
>
> * * foo.goog = ...
> * * /* or */
> * * arguments.callee.goog = ...
> * }
>
> > })();

>
> > if i do goog.__parent__ it gives me null;

>
> > the code:
> > function A(){
> > alert('A');
> > }

>
> > window.A.__parent__ gives me reference to the window.

>
> Consider that a happy coincidence. *(Netscape/Mozilla.org) JavaScript is but
> one of many ECMAScript implementations (see e.g. Microsoft JScript), and
> `window' refers to a host object. *(I have neither ever heard of that
> property before nor did I need to make use of such, and I need two hands to
> count the years I'm doing ECMAScript implementations now. *But thanks for
> mentioning, it will be added to the ECMAScript Support Matrix.)


[snip]

I hadn't either, until Richard mentioned it was in IceBrowser. Can be
used to get the window from the document when other properties are
unavailable (as in IceBrowser.)

Thomas 'PointedEars' Lahn 04-22-2009 05:40 AM

Re: __parent__ property no longer works in firefox as expected
 
kangax wrote:
> Thomas 'PointedEars' Lahn wrote:
>> jgabios wrote:

> [...]
>>> window.goog=Y;

>> Don't augment host objects.

>
> Where's the host object augmentation here? I always thought that a
> global `window` property references a built-in native Global object,
> not a host object.


You always thought wrong. Nothing specifies that to be (remember that
examples in Specifications are not normative), and if the object that this
host-defined property refers to happens to mirror properties of the Global
Object then that's merely a happy coincidence.

> Could you elaborate, please?


Not *again*.

<https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference#Global_Objects>

>>> window.A.__parent__ gives me reference to the window.

>> Consider that a happy coincidence. (Netscape/Mozilla.org) JavaScript is but
>> one of many ECMAScript implementations (see e.g. Microsoft JScript), and
>> `window' refers to a host object. (I have neither ever heard of that
>> property before nor did I need to make use of such, and I need two hands to
>> count the years I'm doing ECMAScript implementations now. But thanks for
>> mentioning, it will be added to the ECMAScript Support Matrix.)

>
> There are other Mozilla non-standard ones, such as __count__,
> __defineGetter__ and __defineSetter__ which don't seem to be in the
> matrix
> <https://developer.mozilla.org/En/Core_JavaScript_1.5_Reference/Global_Objects/Object>


I have the latter two locally already, along with __iterator__ and
__proto__. Thanks for the hint, though.


PointedEars

jgabios 04-22-2009 06:40 AM

Re: __parent__ property no longer works in firefox as expected
 

>
> However, one wonders what do you think you need this for, and why.
>
> PointedEars


i am trying to make a greasemonkey script, on youtube.com, and there
is a function N inside an anonymous function.
alongside with function N, there are other functions inside that
anonymous function that are referenced to the outside code with lines
like this: window.goog.disabled=Y; and so on.
so i can access the Y function inside the anonymous function.
now i wanted to get a reference to the N function by going Y -> parent
-> N as functions are objects.
so, that's my case.
maybe my approach is bad, and cannot be done [i need it to work on
firefox, not interested in other browsers] , do you know any other way
to get to N function?
i also tried to look for a reference of the anonymous function [i know
it is not assigned to a var] but i thought a random property of the
window global object would have been assigned to it. anyway i get
carried away. i stop.

jgabios

Jeremy J Starcher 04-22-2009 05:21 PM

Re: __parent__ property no longer works in firefox as expected
 
On Tue, 21 Apr 2009 23:40:54 -0700, jgabios wrote:


>> However, one wonders what do you think you need this for, and why.
>>
>> PointedEars

>
> i am trying to make a greasemonkey script, on youtube.com, and there is
> a function N inside an anonymous function. alongside with function N,
> there are other functions inside that anonymous function that are
> referenced to the outside code with lines like this:
> window.goog.disabled=Y; and so on. so i can access the Y function inside
> the anonymous function. now i wanted to get a reference to the N
> function by going Y -> parent -> N as functions are objects.
> so, that's my case.



[ Edited to simplify language for OP ]

Assuming you are referring to the 'true' GreaseMonkey and not one of the
other user-script extensions for IE, Chrome, or Konq:

GreaseMonkey has its own private 'window' variable that is separate from
the window the document sees. The 'real' window, if I can call it that,
is referred to as 'unsafeWindow.'


I often include a line line:

var mainWindow = (typeof unsafeWindow !== 'undefined') ? unsafeWindow :
window;

GreaseMonkey scripts are their own world. Javascript, yes, but only a
small part of the DOM is available so they can provide a 'wall' to keep
the document's scripts from looking into into GreaseMonkey private data.



All times are GMT. The time now is 02:47 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.