473,387 Members | 1,749 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.

Function Context

Perhpas somebody can give me a hand with this little problem.

Given:

var object; -- an object
var func; -- a function call back

Now, I want to know how to get the following effect:

object.func = func;
object.func();

function func (somethingelse) {
this.something = somethingelse;
}

such that 'this' referes to 'object' without having to make 'func' a
member of 'object'. I tried `with (object) {func ();}` which I realized
wouldn't do that job.

Thanks,

Brett Foster
Jul 23 '05 #1
10 1583
On Fri, 07 Jan 2005 16:01:40 -0500, Brett Foster
<fo****************@ioctl.ca> wrote:

[snip]
object.func = func;
object.func();

function func (somethingelse) {
this.something = somethingelse;
}


func.call(object);

extending the call to include any arguments that the function, func,
should receive.

If this is for general Internet use (or for any environment that features
any JScript version earlier than 5.5 [which usually means IE5 or
earlier]), you should be prepared to emulate the call method:

if(Function.prototype
&& ('function' != typeof Function.prototype.call))
{
Function.prototype.call = function(o) {var p = '__call', r;
while('undefined' != typeof o[p]) {p += p;}
o[p] = this; r = o[p](); delete o[p]; return r;
};
}

If the object in question might be a host object (such as an element
reference), remove the while statement and delete operator (otherwise IE
will error) and don't use the same property name contained in the local
variable, p.

[snip]

Hope that helps,
Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #2
Michael Winter wrote:
On Fri, 07 Jan 2005 16:01:40 -0500, Brett Foster
<fo****************@ioctl.ca> wrote:

[snip]
object.func = func;
object.func();

function func (somethingelse) {
this.something = somethingelse;
}
func.call(object);

extending the call to include any arguments that the function, func,

should receive.


OK, this is especially interesting in that I was about to ask a quite
similar question. However, I want to know how to use "call()" (or
apply()) from within an object constructor function.

Below is some code. In the second constructor, "FormExam", I'd like to
replace the existing line of code, with a call to either call() or
apply(), to acheive the same effect.

The example in Rhino, as I recall off the top of my head, says
"this.call(...)", but this results in an error message that the method
is not defined. I've also tried FormExam.call(...) and
BaseFormExam.call(....), but these too result in errors. What am I
doing wrong, or do I just need to leave it the way it is below (if it
ain't broke, don't fix it)?

Thanks in advance
function BaseFormExam(_form, _criteria) {
var criteria = _criteria;
var input = _form;

this._passes = function() {
//stub
return false;
}
}

function FormExam(_form, _criteria) {
this.base = BaseFormExam; this.base(_form, _criteria); delete
this.base;
}

Jul 23 '05 #3
On 7 Jan 2005 15:33:46 -0800, Got Scripting? <sc*******@yahoo.com> wrote:

[snip]

As a quick note, it's best to indent and manually wrap code.
function BaseFormExam(_form, _criteria) {
var criteria = _criteria;
var input = _form;
Why don't you use the arguments instead of creating local variables which
contain the same values. There would be no difference in behaviour.

[snip]
function FormExam(_form, _criteria) {
this.base = BaseFormExam; this.base(_form, _criteria);
delete this.base;
}


The same effect would be acheived with

function FormExam(_form, _criteria) {
BaseFormExam.call(this, _form, _criteria);
}

This certainly works in browsers. I don't know about Rhino (assuming
that's your target environment).

Good luck,
Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #4
Michael Winter wrote:
On 7 Jan 2005 15:33:46 -0800, Got Scripting? <sc*******@yahoo.com> wrote:
As a quick note, it's best to indent and manually wrap code.
um, my code simply lost its formatting when cutting and pasting into
google group's textarea (i'll have to keep this in mind for future
posts)
Why don't you use the arguments instead of creating local variables which contain the same values. There would be no difference in behaviour.
ok that's a good idea, see below
function FormExam(_form, _criteria) {
this.base = BaseFormExam; this.base(_form, _criteria);
delete this.base;
}


The same effect would be acheived with

function FormExam(_form, _criteria) {
BaseFormExam.call(this, _form, _criteria);
}


in light of your "good idea" above, then I also decided to try:

BaseFormExam.apply(this, arguments);

since apply takes an array; worked like a charm. I also believe it's
Javascript 1.2 compliant?!
This certainly works in browsers. I don't know about Rhino (assuming that's your target environment).


Your example certainly did work. Sorry about the confusion. I meant
the Rhino book (Flanagan's "javascript: The Definitive Guide"), rather
than the Rhino implementation.

Many thanks!!

Jul 23 '05 #5
On 7 Jan 2005 16:17:26 -0800, Got Scripting? <sc*******@yahoo.com> wrote:
Michael Winter wrote:
[snip]
function FormExam(_form, _criteria) {
BaseFormExam.call(this, _form, _criteria);
}


in light of your "good idea" above, then I also decided to try:

BaseFormExam.apply(this, arguments);

since apply takes an array; worked like a charm. I also believe it's
Javascript 1.2 compliant?!


I don't have a Netscape reference to hand, but something like that.
However, Microsoft didn't implement it until JScript 5.5 (so usually IE5.5
or later). It might be easier to use the call method with the emulation I
showed earlier (though you'll have to modify it slightly to take - and to
use - arguments).
This certainly works in browsers. I don't know about Rhino (assuming
that's your target environment).


I meant the Rhino book (Flanagan's "javascript: The Definitive Guide"),
rather than the Rhino implementation.


It's OK. I had Rhino (Moz) stuck in my head from an earlier thread. :)
Many thanks!!


You're welcome.

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #6
Michael Winter wrote:
On 7 Jan 2005 16:17:26 -0800, Got Scripting? <sc*******@yahoo.com> wrote:
function FormExam(_form, _criteria) {
BaseFormExam.call(this, _form, _criteria);
}
in light of your "good idea" above, then I also decided to try:

BaseFormExam.apply(this, arguments);

since apply takes an array; worked like a charm. I also believe it's Javascript 1.2 compliant?!


I don't have a Netscape reference to hand, but something like that.
However, Microsoft didn't implement it until JScript 5.5 (so usually

IE5.5 or later). It might be easier to use the call method with the emulation I showed earlier (though you'll have to modify it slightly to take - and to use - arguments).


Well this has certainly been enlightening. apply() seems the most
elegant for my particular usage, but the implementation limitations of
apply() and call() may just make me stick with my original:

this.base = BaseFormExam; this.base(_form, _criteria); delete
this.base;

Inelegant? Yes. But to me, simpler than hacking Function's prototype.
Though its still good to learn different approaches ;)

Jul 23 '05 #7
Michael Winter wrote:
<snip>
if(Function.prototype
&& ('function' != typeof Function.prototype.call))
{
Function.prototype.call = function(o) {var p = '__call', r;
while('undefined' != typeof o[p]) {p += p;}
o[p] = this; r = o[p](); delete o[p]; return r;
};
}

If the object in question might be a host object (such as an
element reference), remove the while statement and delete
operator (otherwise IE will error) and don't use the same
property name contained in the local variable, p.

<snip>

As I recall, the problem with IE's host objects is that they error on
the delete statement. I wonder whether assigning - Undefined - (the
current value of an unassigned local variable should do for that) would
be sufficient. In the event that the object did already have a property
name that coincided with (possibly repeated) '__call' then it would have
to be - Undefined - at the start of the execution of the - call -
emulation, and would be re-assigned - Undefined - at the end, which
should be (mostly) harmless.

Richard.

Jul 23 '05 #8
On Sat, 8 Jan 2005 12:31:12 -0000, Richard Cornford
<Ri*****@litotes.demon.co.uk> wrote:

[snip]
As I recall, the problem with IE's host objects is that they error on
the delete statement.


Yes, that's what I was trying to imply. Evidently Microsoft don't bother
with the internal attributes set forth in ECMA-262 and treat all
properties on host objects as DontDelete. Why they had to flag an error
rather than returning false is beyond me, though.

[alternative deletion suggestion]

if(Function.prototype
&& ('function' != typeof Function.prototype.call))
{
Function.prototype.call = function(o) {var p = '__call', r, u;
while('undefined' != typeof o[p]) {p += p;}
o[p] = this; r = o[p](); o[p] = u; return r;
};
}

An alternative to an uninitialised local variable would be void operator
which always evaluates to undefined.

This might be an odd question to ask, Richard, but am I checking for the
prototype object for any particular reason? I'm sure there *is* a reason -
and I'd assume that it's due to the provisions of ECMA-327 - but I've
completely forgotten. It's just become something I feel compelled to do.

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #9
Michael Winter wrote:
<snip>
This might be an odd question to ask, Richard, but am I
checking for the prototype object for any particular reason?
I'm sure there *is* a reason - and I'd assume that it's due
to the provisions of ECMA-327 - but I've completely forgotten.
It's just become something I feel compelled to do.

<snip>

It would take either a very odd, or extremely old, script implementation
for the Function.prototype to be missing (and the old ones are so old
that you wouldn't have - typeof - to test with anyway). However,
verifying Function.prototype doesn't seem a bad thing to be doing,
though logic would suggest verifying - Function - as well. It will
prevent the script from erring-out at the following test and is only
done once. Though if either Function or Function.prototype were missing
then the script would likely error-out when call was later executed.

Richard.
Jul 23 '05 #10
Michael Winter wrote:
On Fri, 07 Jan 2005 16:01:40 -0500, Brett Foster
<fo****************@ioctl.ca> wrote:

[snip]
object.func = func;
object.func();

function func (somethingelse) {
this.something = somethingelse;
}

func.call(object);

extending the call to include any arguments that the function, func,
should receive.

If this is for general Internet use (or for any environment that
features any JScript version earlier than 5.5 [which usually means IE5
or earlier]), you should be prepared to emulate the call method:

if(Function.prototype
&& ('function' != typeof Function.prototype.call))
{
Function.prototype.call = function(o) {var p = '__call', r;
while('undefined' != typeof o[p]) {p += p;}
o[p] = this; r = o[p](); delete o[p]; return r;
};
}

If the object in question might be a host object (such as an element
reference), remove the while statement and delete operator (otherwise
IE will error) and don't use the same property name contained in the
local variable, p.

[snip]

Hope that helps,
Mike


Thanks!!!

Brett
Jul 23 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) {...
3
by: jimjim | last post by:
Hello, My question concerns as to how a pointer is passed by reference as a function argument. The following is from code taken from the MICO implementation of the CORBA specification. in...
1
by: Bryan Parkoff | last post by:
I know how to write "Pointer to Function" inside struct or class without using static, but I have decided to add static to all functions inside struct or class because I want member functions to be...
26
by: Adam Warner | last post by:
Hello all, I'm very new to C but I have a number of years of Common Lisp programming experience. I'm trying to figure out ways of translating higher order concepts such as closures into C. The...
4
by: anonymous | last post by:
Thanks your reply. The article I read is from www.hakin9.org/en/attachments/stackoverflow_en.pdf. And you're right. I don't know it very clearly. And that's why I want to understand it; for it's...
11
by: Marco Loskamp | last post by:
Dear list, I'm trying to dynamically generate functions; it seems that what I really want is beyond C itself, but I'd like to be confirmed here. In the minimal example below, I'd like to...
27
by: Marlene Stebbins | last post by:
I am experimenting with function pointers. Unfortunately, my C book has nothing on function pointers as function parameters. I want to pass a pointer to ff() to f() with the result that f() prints...
28
by: Larax | last post by:
Best explanation of my question will be an example, look below at this simple function: function SetEventHandler(element) { // some operations on element element.onclick = function(event) {
4
by: alex | last post by:
I am so confused with these three concept,who can explained it?thanks so much? e.g. var f= new Function("x", "y", "return x * y"); function f(x,y){ return x*y } var f=function(x,y){
6
by: smmk25 | last post by:
Before I state the problem, I just want to let the readers know, I am knew to C++\CLI and interop so please forgive any newbie questions. I have a huge C library which I want to be able to use in...
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: 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?
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.