473,387 Members | 3,781 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,387 software developers and data experts.

Override referenced functions

Hi,

Say I have already executed the following code in a JavaScript interpreter:
Expand|Select|Wrap|Line Numbers
  1. var x,o1,o2;
  2. x=function(){ alert(this.z); };
  3. o1={z:"o1",t:"o1t"};
  4. o1.alertfunc=x;
  5. o2={z:"o2",t:"o2t"};
  6. o2.alertfunc=x;
  7. o1.alertfunc(); // alerts "o1"
  8. o2.alertfunc(); // alerts "o2"
How do I change the function referenced by x, o1.alertfunc and o2.alertfunc to
Expand|Select|Wrap|Line Numbers
  1. function(){ alert(this.t); }
without doing the reassignments all over again?

I know a workaround would be to set x to call another function which I can then modify when necessary, but thought that there might be a better way.
Sep 30 '11 #1

✓ answered by gits

what do you mean exactly with your workaround? the following will always output 'o1':

Expand|Select|Wrap|Line Numbers
  1. var x,o1,o2;
  2. x=function(){ alert(this.z); };
  3. o1={z:"o1",t:"o1t"};
  4. o1.alertfunc=x;
  5. o2={z:"o2",t:"o2t"};
  6. o2.alertfunc=x;
  7. o1.alertfunc(); // alerts "o1"
  8. o2.alertfunc(); // alerts "o2"
  9.  
  10. // do you mean that?
  11. x=function(){ alert(this.t); };
  12. o1.alertfunc(); 
  13.  
  14. // --> outputs 'o1' - functions aren't passed by reference
  15.  
  16. // so you would need to reassign the func to o1 like:
  17. o1.alertfunc = x;
  18. o1.alertfunc();
  19.  
  20. // --> now it outputs 'o1t' - which is a quite, hmmm,
  21. // 'ugly' way of doing such things :)
  22.  
so you would have to use a bit OO magic :) like this:

Expand|Select|Wrap|Line Numbers
  1. var x,o1,o2;
  2.  
  3. // we use an obj and its prototype to declare a func
  4. function baseObj() {}
  5.  
  6. baseObj.prototype.x = function() {
  7.     alert(this.z);
  8. };
  9.  
  10. o1 = {
  11.     z: "o1",
  12.     t: "o1t"
  13. };
  14.  
  15. // we assign the function and bind the scope 
  16. o1.alertfunc = function() {
  17.     baseObj.prototype.x.apply(this, arguments);
  18. }
  19.  
  20. o1.alertfunc(); // --> outputs 'o1'
  21.  
  22. // we modify our basObj's prototype 
  23. baseObj.prototype.x = function() {
  24.     alert(this.t);
  25. };
  26.  
  27. o1.alertfunc(); // --> now output is: 'o1t'
  28.  
kind regards

3 1120
gits
5,390 Expert Mod 4TB
what do you mean exactly with your workaround? the following will always output 'o1':

Expand|Select|Wrap|Line Numbers
  1. var x,o1,o2;
  2. x=function(){ alert(this.z); };
  3. o1={z:"o1",t:"o1t"};
  4. o1.alertfunc=x;
  5. o2={z:"o2",t:"o2t"};
  6. o2.alertfunc=x;
  7. o1.alertfunc(); // alerts "o1"
  8. o2.alertfunc(); // alerts "o2"
  9.  
  10. // do you mean that?
  11. x=function(){ alert(this.t); };
  12. o1.alertfunc(); 
  13.  
  14. // --> outputs 'o1' - functions aren't passed by reference
  15.  
  16. // so you would need to reassign the func to o1 like:
  17. o1.alertfunc = x;
  18. o1.alertfunc();
  19.  
  20. // --> now it outputs 'o1t' - which is a quite, hmmm,
  21. // 'ugly' way of doing such things :)
  22.  
so you would have to use a bit OO magic :) like this:

Expand|Select|Wrap|Line Numbers
  1. var x,o1,o2;
  2.  
  3. // we use an obj and its prototype to declare a func
  4. function baseObj() {}
  5.  
  6. baseObj.prototype.x = function() {
  7.     alert(this.z);
  8. };
  9.  
  10. o1 = {
  11.     z: "o1",
  12.     t: "o1t"
  13. };
  14.  
  15. // we assign the function and bind the scope 
  16. o1.alertfunc = function() {
  17.     baseObj.prototype.x.apply(this, arguments);
  18. }
  19.  
  20. o1.alertfunc(); // --> outputs 'o1'
  21.  
  22. // we modify our basObj's prototype 
  23. baseObj.prototype.x = function() {
  24.     alert(this.t);
  25. };
  26.  
  27. o1.alertfunc(); // --> now output is: 'o1t'
  28.  
kind regards
Oct 5 '11 #2
Yes, I actually meant something like that but didn't realise you would need to use "apply" to make it work. Thanks.

So I assume creating new functions is the only way to pass functions by reference? Wouldn't that be quite inefficient...?

Also, why do you need to use an object? I thought you could just replace "baseObj.prototype.x" with "myFunctionName" and it would also work?
Oct 5 '11 #3
gits
5,390 Expert Mod 4TB
by using an object you might use much better code then the shown one - i used the obj above to give you some idea of it. if i had to realise something like your example i would use code like the below:

Expand|Select|Wrap|Line Numbers
  1. function baseObj(params) {
  2.     for (var i in params) {
  3.         this[i] = params[i];
  4.     }
  5. }
  6.  
  7. baseObj.prototype.alertfunc = function(p) {
  8.     alert(this[p]);
  9. };
  10.  
  11. var o = new baseObj({z: 'o1', t: 'o1t'});
  12.  
  13. o.alertfunc('z');
  14. o.alertfunc('t');
  15.  
there is only one func and you could easyly extend it etc.

kind regards
Oct 5 '11 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: | last post by:
If in my project I have a function which is not called, does linker add this function from object file to final binary file? (not a lib, an executable) If yes, why? (function has not 'extern') ...
15
by: Jerry He | last post by:
Hi, Is it possible to create a function that you can use without parenthesizing the arguments? for example, for def examine(str): ..... ..... Is there some way to define it so that I can...
4
by: Office Drone | last post by:
I already figured out how to replace new & delete operators, which wasn't that hard and this is working great globally. However, aside from new & delete, memory allocation is also used via...
39
by: Geronimo W. Christ Esq | last post by:
Are there any scripts or tools out there that could look recursively through a group of C/C++ source files, and allow unreferenced function calls or values to be easily identified ? LXR is handy...
1
by: | last post by:
In Actionscript 2.0 there is the concept of dynamically generating and naming objects at runtime. Dynamically named objects can be referenced and manipulated programmatically using array-access...
1
by: Grant Frisken | last post by:
I have a function in a Managed C++ base class which looks something like: public __gc class Foo { virtual A* Bar(B* b, C*c); }; where A is an unmanaged class and B and C are managed classes....
7
by: Bob Stearns | last post by:
Is there an option in php to do a 'require xxx.php' if, when a function call to xxx is encountered, it is not defined? It would look in all the standard places.
7
by: Nick Keighley | last post by:
I take it this is wrong:- class Direct_draw { public: Direct_draw (); virtual ~Direct_draw () {}
14
by: v4vijayakumar | last post by:
Why we need "virtual private member functions"? Why it is not an (compile time) error?
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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:
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.