how can I overwrite a prototype function?

H

hzgt9b

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?
 
T

Thomas 'PointedEars' Lahn

hzgt9b said:
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
 
H

hzgt9b

hzgt9b said:
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.
 

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

Forum statistics

Threads
473,780
Messages
2,569,608
Members
45,241
Latest member
Lisa1997

Latest Threads

Top