Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Function Context

Reply
Thread Tools

Function Context

 
 
Brett Foster
Guest
Posts: n/a
 
      01-07-2005
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
 
Reply With Quote
 
 
 
 
Michael Winter
Guest
Posts: n/a
 
      01-07-2005
On Fri, 07 Jan 2005 16:01:40 -0500, Brett Foster
<(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
 
Got Scripting?
Guest
Posts: n/a
 
      01-07-2005
Michael Winter wrote:
> On Fri, 07 Jan 2005 16:01:40 -0500, Brett Foster
> <(E-Mail Removed)> 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;
}

 
Reply With Quote
 
Michael Winter
Guest
Posts: n/a
 
      01-07-2005
On 7 Jan 2005 15:33:46 -0800, Got Scripting? <(E-Mail Removed)> 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.
 
Reply With Quote
 
Got Scripting?
Guest
Posts: n/a
 
      01-08-2005
Michael Winter wrote:
> On 7 Jan 2005 15:33:46 -0800, Got Scripting? <(E-Mail Removed)>

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!!

 
Reply With Quote
 
Michael Winter
Guest
Posts: n/a
 
      01-08-2005
On 7 Jan 2005 16:17:26 -0800, Got Scripting? <(E-Mail Removed)> 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.
 
Reply With Quote
 
Got Scripting?
Guest
Posts: n/a
 
      01-08-2005
Michael Winter wrote:
> On 7 Jan 2005 16:17:26 -0800, Got Scripting? <(E-Mail Removed)>

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

 
Reply With Quote
 
Richard Cornford
Guest
Posts: n/a
 
      01-08-2005
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.



 
Reply With Quote
 
Michael Winter
Guest
Posts: n/a
 
      01-08-2005
On Sat, 8 Jan 2005 12:31:12 -0000, Richard Cornford
<(E-Mail Removed)> 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.
 
Reply With Quote
 
Richard Cornford
Guest
Posts: n/a
 
      01-08-2005
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.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Function versus pointer to function, in context of std::function,huh? Alf P. Steinbach C++ 10 07-27-2011 05:51 AM
return HttpStatusCode.ServiceUnavailable in the context.context.Response.StatusCode (Http response code 503) Flip Rayner ASP .Net 1 01-23-2007 06:35 AM
How to retrieve a session Bean in the Context in a class with the Context reference asd Java 1 11-09-2006 05:00 PM
Strange Context Error: Context 0x197ee0 is disconnected in VS 2005 =?Utf-8?B?U3VuU21pbGU=?= ASP .Net 0 01-10-2006 03:59 PM
Context.Items vs Context.Handler (passing values between pages) VS_NET_DEV ASP .Net 2 05-25-2004 01:16 PM



Advertisments