By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,453 Members | 956 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,453 IT Pros & Developers. It's quick & easy.

how can I overwrite a prototype function?

P: n/a
I know how to overwrite a function. Normally this is what I would do:
function someFunction() { /* orig definition here */ }
//later in the execution stream I would do...
someFunction = function () { /* overwrite function definition */ }
The above works fine for me even when someFunction is originally
defined in a seperate frame other than the code that overwrites it
(obviously on the same domain).

What I don't know how to-do is overwrite a prototype function that
already loaded into memory on another frame (on the same domain). For
example:

//say, this prototype function is defined on a frame named
'frame1'
String.prototype.someOtherFunction() { /* define prototype
function here */ }

Problem is I don't know how to access the prototype function from
another frame. I've tried
//assume we are not (executing) in 'frame1'
top.frame1.String.prototype.someOtherFunction = function () { /*
overwrite function here */ }
top.frame1.someOtherFunction = function () { /* overwrite
function here */ } //this just defines a new function in frame1

Is this possible? Can someone give me some pointers?
Jun 27 '08 #1
Share this Question
Share on Google+
2 Replies


P: n/a
hzgt9b wrote:
I know how to overwrite a function. Normally this is what I would do:
function someFunction() { /* orig definition here */ }
//later in the execution stream I would do...
someFunction = function () { /* overwrite function definition */ }
The above works fine for me even when someFunction is originally
defined in a seperate frame other than the code that overwrites it
(obviously on the same domain).

What I don't know how to-do is overwrite a prototype function that
already loaded into memory on another frame (on the same domain). For
example:

//say, this prototype function is defined on a frame named
'frame1'
String.prototype.someOtherFunction() { /* define prototype
function here */ }
This is a syntax error. Prototype methods are defined like this instead:

String.prototype.someOtherFunction = function() {
// ...
};

Or like this, but that would overwrite all other methods and so is useful
for initialization of user-defined prototypes only:

String.prototype = {
someOtherFunction: function() {
// ...
}
};

You should avoid augmenting prototype objects of built-in objects for it
complicates programming a lot; String.prototype could be considered an
exception to this rule of thumb because for-in iteration over strings does
not yield useful results in all implementations and so one would probably
not use for-in then.
Problem is I don't know how to access the prototype function from
another frame.
window.parent.frames["frame1"].Foo.prototype.bar
or
window.top.frames["frame1"].Foo.prototype.bar

Depends on how deep your frameset is nested and where the method is defined.
I've tried
//assume we are not (executing) in 'frame1'
top.frame1.String.prototype.someOtherFunction = function () { /*
overwrite function here */ }
This would accomplish what you want, but it would only affect the global
execution context of top.frame1, of course.
top.frame1.someOtherFunction = function () { /* overwrite
function here */ } //this just defines a new function in frame1
Actually, it only adds a property to top.frame1. It is
implementation-dependent whether this property will be available as a method
of the Global Object of the global execution context top.frame1 represents.
Is this possible? Can someone give me some pointers?
It does not make sense to overwrite a method in another global execution
context, unless this method is used in that execution context or subordered
local contexts.
PointedEars
--
Use any version of Microsoft Frontpage to create your site.
(This won't prevent people from viewing your source, but no one
will want to steal it.)
-- from <http://www.vortex-webdesign.com/help/hidesource.htm>
Jun 27 '08 #2

P: n/a
On Jun 7, 2:03*pm, Thomas 'PointedEars' Lahn <PointedE...@web.de>
wrote:
hzgt9b wrote:
I know how to overwrite a function. Normally this is what I would do:
* * function someFunction() { /* orig definition here */ }
* * //later in the execution stream I would do...
* * someFunction = function () { /* overwrite function definition */ }
The above works fine for me even when someFunction is originally
defined in a seperate frame other than the code that overwrites it
(obviously on the same domain).
What I don't know how to-do is overwrite a prototype function that
already loaded into memory on another frame (on the same domain). For
example:
* * *//say, this prototype function is defined on a frame named
'frame1'
* * *String.prototype.someOtherFunction() { /* define prototype
function here */ }

This is a syntax error. *Prototype methods are defined like this instead:

* String.prototype.someOtherFunction = function() {
* * // ...
* };

Or like this, but that would overwrite all other methods and so is useful
for initialization of user-defined prototypes only:

* String.prototype = {
* * someOtherFunction: function() {
* * * // ...
* * }
* };

You should avoid augmenting prototype objects of built-in objects for it
complicates programming a lot; String.prototype could be considered an
exception to this rule of thumb because for-in iteration over strings does
not yield useful results in all implementations and so one would probably
not use for-in then.
Problem is I don't know how to access the prototype function from
another frame.

* window.parent.frames["frame1"].Foo.prototype.bar
or
* window.top.frames["frame1"].Foo.prototype.bar

Depends on how deep your frameset is nested and where the method is defined.
I've tried
* * *//assume we are not (executing) in 'frame1'
* * *top.frame1.String.prototype.someOtherFunction = function (){ /*
overwrite function here */ }

This would accomplish what you want, but it would only affect the global
execution context of top.frame1, of course.
* * *top.frame1.someOtherFunction = function () { /* overwrite
function here */ } //this just defines a new function in frame1

Actually, it only adds a property to top.frame1. *It is
implementation-dependent whether this property will be available as a method
of the Global Object of the global execution context top.frame1 represents..
Is this possible? Can someone give me some pointers?

It does not make sense to overwrite a method in another global execution
context, unless this method is used in that execution context or subordered
local contexts.

PointedEars
--
Use any version of Microsoft Frontpage to create your site.
(This won't prevent people from viewing your source, but no one
will want to steal it.)
* -- from <http://www.vortex-webdesign.com/help/hidesource.htm>- Hide quoted text -

- Show quoted text -
Thanks for the reply and info.
Jun 27 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.