Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #14591

Re: Function Objects

From Stefan Weiss <krewecherl@gmail.com>
Newsgroups comp.lang.javascript
Subject Re: Function Objects
Date 2012-06-20 17:29 +0200
Organization albasani.net
Message-ID <jrsq8a$li8$1@news.albasani.net> (permalink)
References <bm22u7l5dbs0rrdeloru3fd3sct7n8g201@4ax.com> <jrr4cc$k5u$1@news.albasani.net>

Show all headers | View raw


On 2012-06-20 02:09, Stefan Weiss wrote:
>     var dog = {
>         noise: "wow!",
>         bark: function () { alert(this.noise); }
>     };
> 
>     dog.bark();  // "wow!"
> 
>     var alias = dog.bark;
> 
>     alias();  // "undefined"

I should have mentioned that there is more than one way to (re-)connect
the bark() function with the dog object:

    alias.call(dog);   // "wow!"
    alias.apply(dog);  // "wow!"

    var alias2 = dog.bark.bind(dog);
    alias2();  // "wow!"

The difference between call() and apply() is the way parameters are
passed to the function; since bark() has no parameters, they look the
same in this example.
Function.prototype.bind is not available in older browsers, but can be
retro-fitted (with some caveats).

- stefan

Back to comp.lang.javascript | Previous | Next — Previous in thread | Find similar | Unroll thread


Thread

Function Objects Gene Wirchenko <genew@ocis.net> - 2012-06-19 16:35 -0700
  Re: Function Objects Stefan Weiss <krewecherl@gmail.com> - 2012-06-20 02:09 +0200
    Re: Function Objects Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-06-20 15:43 +0200
    Re: Function Objects Stefan Weiss <krewecherl@gmail.com> - 2012-06-20 17:29 +0200

csiph-web