473,378 Members | 1,383 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

how can I overwrite a prototype function?

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
2 6433
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Elf M. Sternberg | last post by:
One of the complaints about prototype.js (google for it if you're not familiar with it) is that it's poorly documented. I have this inkling that the key to understanding prototype.js is in the...
8
by: Robert | last post by:
Hi, I can use "with" like this: function MyObject(message) { this.message = message; } function _MyObject_speak() {
2
by: stephane | last post by:
Hi all, What I am trying to achieve is an 'inherits' method similar to Douglas Crockford's (http://www.crockford.com/javascript/inheritance.html) but that can enable access to the superclass'...
4
by: lkrubner | last post by:
I'm reading an essay, I think one of Crockford's, and it has this example in it: function Demo() { } Demo.prototype = new Ancestor(); Demo.prototype.foo = function () { } ; Does Ancestor now...
21
by: Rob Somers | last post by:
Hey people, I read a good thread on here regarding the reason why we use function prototypes, and it answered most of my questions, but I wanted to double check on a couple of things, as I am...
13
by: eman1000 | last post by:
I was recently looking at the prototype library (http://prototype.conio.net/) and I noticed the author used the following syntax: Object.extend(MyObj.prototype, { my_meth1: function(){},...
5
by: Daz | last post by:
Hi everyone. My query is very straight forward (I think). What's the difference between someFunc.blah = function(){ ; } and
6
by: burningodzilla | last post by:
Hi all - I'm preparing to dive in to more complex application development using javascript, and among other things, I'm having a hard time wrapping my head around an issues regarding "inheritance"...
3
by: jacobstr | last post by:
I've noticed Object.extend used in a few different ways and I'm having trouble distinguishing why certain usages apply to a given situation. On line 804 Ajax.Base is defined as follows: ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.