Function Context

B

Brett Foster

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
 
M

Michael Winter

On Fri, 07 Jan 2005 16:01:40 -0500, Brett Foster

[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
 
G

Got Scripting?

Michael said:
On Fri, 07 Jan 2005 16:01:40 -0500, Brett Foster

[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;
}
 
M

Michael Winter

[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
 
G

Got Scripting?

Michael said:
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
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!!
 
M

Michael Winter

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).
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
 
G

Got Scripting?

Michael said:
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 ;)
 
R

Richard Cornford

Michael Winter wrote:
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.
 
M

Michael Winter

On Sat, 8 Jan 2005 12:31:12 -0000, Richard Cornford

[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
 
R

Richard Cornford

Michael Winter wrote:
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.
 
B

Brett Foster

Michael said:
On Fri, 07 Jan 2005 16:01:40 -0500, Brett Foster

[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
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top