Function Context

Discussion in 'Javascript' started by Brett Foster, Jan 7, 2005.

  1. Brett Foster

    Brett Foster Guest

    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
    Brett Foster, Jan 7, 2005
    #1
    1. Advertising

  2. On Fri, 07 Jan 2005 16:01:40 -0500, Brett Foster
    <> 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.
    Michael Winter, Jan 7, 2005
    #2
    1. Advertising

  3. Michael Winter wrote:
    > On Fri, 07 Jan 2005 16:01:40 -0500, Brett Foster
    > <> 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;
    }
    Got Scripting?, Jan 7, 2005
    #3
  4. On 7 Jan 2005 15:33:46 -0800, Got Scripting? <> 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.
    Michael Winter, Jan 7, 2005
    #4
  5. Michael Winter wrote:
    > On 7 Jan 2005 15:33:46 -0800, Got Scripting? <>

    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!!
    Got Scripting?, Jan 8, 2005
    #5
  6. On 7 Jan 2005 16:17:26 -0800, Got Scripting? <> 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.
    Michael Winter, Jan 8, 2005
    #6
  7. Michael Winter wrote:
    > On 7 Jan 2005 16:17:26 -0800, Got Scripting? <>

    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 ;)
    Got Scripting?, Jan 8, 2005
    #7
  8. 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.
    Richard Cornford, Jan 8, 2005
    #8
  9. On Sat, 8 Jan 2005 12:31:12 -0000, Richard Cornford
    <> 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.
    Michael Winter, Jan 8, 2005
    #9
  10. 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.
    Richard Cornford, Jan 8, 2005
    #10
  11. Brett Foster

    Brett Foster Guest

    Michael Winter wrote:
    > On Fri, 07 Jan 2005 16:01:40 -0500, Brett Foster
    > <> 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
    Brett Foster, Jan 8, 2005
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. VS_NET_DEV
    Replies:
    2
    Views:
    3,798
    jenny
    May 25, 2004
  2. =?Utf-8?B?U3VuU21pbGU=?=
    Replies:
    0
    Views:
    704
    =?Utf-8?B?U3VuU21pbGU=?=
    Jan 10, 2006
  3. Flip Rayner
    Replies:
    1
    Views:
    652
    bruce barker
    Jan 23, 2007
  4. asd
    Replies:
    1
    Views:
    432
    www.pulpjava.com
    Nov 9, 2006
  5. Alf P. Steinbach
    Replies:
    10
    Views:
    3,095
    Alf P. Steinbach
    Jul 27, 2011
Loading...

Share This Page