Hi,
Say I have already executed the following code in a JavaScript interpreter: -
var x,o1,o2;
-
x=function(){ alert(this.z); };
-
o1={z:"o1",t:"o1t"};
-
o1.alertfunc=x;
-
o2={z:"o2",t:"o2t"};
-
o2.alertfunc=x;
-
o1.alertfunc(); // alerts "o1"
-
o2.alertfunc(); // alerts "o2"
How do I change the function referenced by x, o1.alertfunc and o2.alertfunc to - 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.
what do you mean exactly with your workaround? the following will always output 'o1': -
var x,o1,o2;
-
x=function(){ alert(this.z); };
-
o1={z:"o1",t:"o1t"};
-
o1.alertfunc=x;
-
o2={z:"o2",t:"o2t"};
-
o2.alertfunc=x;
-
o1.alertfunc(); // alerts "o1"
-
o2.alertfunc(); // alerts "o2"
-
-
// do you mean that?
-
x=function(){ alert(this.t); };
-
o1.alertfunc();
-
-
// --> outputs 'o1' - functions aren't passed by reference
-
-
// so you would need to reassign the func to o1 like:
-
o1.alertfunc = x;
-
o1.alertfunc();
-
-
// --> now it outputs 'o1t' - which is a quite, hmmm,
-
// 'ugly' way of doing such things :)
-
so you would have to use a bit OO magic :) like this: -
var x,o1,o2;
-
-
// we use an obj and its prototype to declare a func
-
function baseObj() {}
-
-
baseObj.prototype.x = function() {
-
alert(this.z);
-
};
-
-
o1 = {
-
z: "o1",
-
t: "o1t"
-
};
-
-
// we assign the function and bind the scope
-
o1.alertfunc = function() {
-
baseObj.prototype.x.apply(this, arguments);
-
}
-
-
o1.alertfunc(); // --> outputs 'o1'
-
-
// we modify our basObj's prototype
-
baseObj.prototype.x = function() {
-
alert(this.t);
-
};
-
-
o1.alertfunc(); // --> now output is: 'o1t'
-
kind regards
3 1120 gits 5,390
Expert Mod 4TB
what do you mean exactly with your workaround? the following will always output 'o1': -
var x,o1,o2;
-
x=function(){ alert(this.z); };
-
o1={z:"o1",t:"o1t"};
-
o1.alertfunc=x;
-
o2={z:"o2",t:"o2t"};
-
o2.alertfunc=x;
-
o1.alertfunc(); // alerts "o1"
-
o2.alertfunc(); // alerts "o2"
-
-
// do you mean that?
-
x=function(){ alert(this.t); };
-
o1.alertfunc();
-
-
// --> outputs 'o1' - functions aren't passed by reference
-
-
// so you would need to reassign the func to o1 like:
-
o1.alertfunc = x;
-
o1.alertfunc();
-
-
// --> now it outputs 'o1t' - which is a quite, hmmm,
-
// 'ugly' way of doing such things :)
-
so you would have to use a bit OO magic :) like this: -
var x,o1,o2;
-
-
// we use an obj and its prototype to declare a func
-
function baseObj() {}
-
-
baseObj.prototype.x = function() {
-
alert(this.z);
-
};
-
-
o1 = {
-
z: "o1",
-
t: "o1t"
-
};
-
-
// we assign the function and bind the scope
-
o1.alertfunc = function() {
-
baseObj.prototype.x.apply(this, arguments);
-
}
-
-
o1.alertfunc(); // --> outputs 'o1'
-
-
// we modify our basObj's prototype
-
baseObj.prototype.x = function() {
-
alert(this.t);
-
};
-
-
o1.alertfunc(); // --> now output is: 'o1t'
-
kind regards
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?
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: -
function baseObj(params) {
-
for (var i in params) {
-
this[i] = params[i];
-
}
-
}
-
-
baseObj.prototype.alertfunc = function(p) {
-
alert(this[p]);
-
};
-
-
var o = new baseObj({z: 'o1', t: 'o1t'});
-
-
o.alertfunc('z');
-
o.alertfunc('t');
-
there is only one func and you could easyly extend it etc.
kind regards
Sign in to post your reply or Sign up for a free account.
Similar topics
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')
...
|
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...
|
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...
|
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...
|
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...
|
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....
|
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.
|
by: Nick Keighley |
last post by:
I take it this is wrong:-
class Direct_draw
{
public:
Direct_draw ();
virtual ~Direct_draw ()
{}
|
by: v4vijayakumar |
last post by:
Why we need "virtual private member functions"? Why it is not an
(compile time) error?
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
|
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...
| |