Web Images Videos Maps News Shopping Gmail more »
Recently Visited Groups | Help | Sign in
Google Groups Home
Augmenting functions
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  7 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
disappearedng  
View profile  
 More options Nov 12 2008, 9:28 pm
Newsgroups: comp.lang.javascript
From: disappearedng <disappeare...@gmail.com>
Date: Wed, 12 Nov 2008 18:28:42 -0800 (PST)
Local: Wed, Nov 12 2008 9:28 pm
Subject: Augmenting functions
Hi everyone,
I am reading Javascript the good parts and came across this that I
don't understand:

Function.prototype.method = function ( name, func ) {
                        this.prototype[name] = func;
                        return this;
                        };

Number.method('integer' , function()    {
                        return Math[this < 0 ? 'ceiling' : 'floor'](this);
                        });

My question is: what the hell is Function.prototype.method doing?
2nd What the hell is Number doing?

Why do we have to go Function.prototype.method instead of just
Function.method? What does the prototype serve here?


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
RobG  
View profile  
(1 user)  More options Nov 12 2008, 10:30 pm
Newsgroups: comp.lang.javascript
From: RobG <rg...@iinet.net.au>
Date: Wed, 12 Nov 2008 19:30:19 -0800 (PST)
Local: Wed, Nov 12 2008 10:30 pm
Subject: Re: Augmenting functions
On Nov 13, 12:28 pm, disappearedng <disappeare...@gmail.com> wrote:

> Hi everyone,
> I am reading Javascript the good parts and came across this that I
> don't understand:

I haven't read it yet, but given what other stuff I've seen of
Crockford's he's fairly concise and assumes a reasonable level of
javascript knowledge.

> Function.prototype.method = function ( name, func ) {
>                         this.prototype[name] = func;
>                         return this;
>                         };

> Number.method('integer' , function()    {
>                         return Math[this < 0 ? 'ceiling' : 'floor'](this);
>                         });

> My question is: what the hell is Function.prototype.method doing?

The code creates a property of Function.prototype called method.  It
is assigned the function on the right hand side of the assignment
operator.

The function creates a property of the object referenced by its this
keyword with a name that is the value of the 'name' parameter and
assigns it a reference to whatever the value of func is (in this case,
it is expected to be a reference to a function).  It returns the value
of its this keyword (I guess for convenience if it's required, it can
be ignored).

If I'd written the above, I'd probably have used the name 'addMethod'
rather than 'method'.

> 2nd What the hell is Number doing?

Number isn't "doing" anything.  Number's internal [[prototype]]
property references Function.prototype, effectively making it an
instance of Function[1].  Because Function.prototype is on Number's
prototype chain, Number will "inherit" methods added to it (as will
all instances of Function).  So adding method to Function.prototype
means that it will be available to Number as Number.method (and to
Array as Array.method and so on).

To be clear, when calling Number.method, the identifier 'method' will
be checked to see if it is a property of Number.  If not found,
Number's internal [[prototype]] property will be checked - it
references Function.prototype, which has the newly added 'method' as a
property, so method is called with the value of its this keyword set
as a reference to Number.

> Why do we have to go Function.prototype.method instead of just
> Function.method? What does the prototype serve here?

Methods added to Number.prototype are "inherited" by instances of
Number.  Methods added to Number belong to Number only, they aren't
inherited by other objects.  Given that all constructors (and most
built-in objects are constructors) are instances of Function, I guess
Crockford wants all such objects to inherit the method function.

The method function added to Function.prototype provides a convenient
way of adding methods to any native constructor's prototype (including
both built-in and user-defined ones) and is effectively equivalent to
using <constructor>.prototype.  The methods added with method will be
inherited by instances created by the constructors.

e.g.

  function func(){}
  var functionRef = func;

  function Foo(){}
  Foo.method('methodName0', functionRef);
  Foo.method('methodName1', function() {/*...*/});

  var foo = new Foo();

  alert(typeof foo.methodName0) // -> function
  alert(typeof foo.methodName1) // -> function

1. Quite a few of the built-in objects in javascript are set up this
way, such as Object, Array, String and others.

--
Rob


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
RobG  
View profile  
 More options Nov 13 2008, 1:48 am
Newsgroups: comp.lang.javascript
From: RobG <rg...@iinet.net.au>
Date: Wed, 12 Nov 2008 22:48:12 -0800 (PST)
Local: Thurs, Nov 13 2008 1:48 am
Subject: Re: Augmenting functions
On Nov 13, 1:30 pm, RobG <rg...@iinet.net.au> wrote:

> On Nov 13, 12:28 pm, disappearedng <disappeare...@gmail.com> wrote:
[...]
> > Why do we have to go Function.prototype.method instead of just
> > Function.method? What does the prototype serve here?

> Methods added to Number.prototype are "inherited" by instances of
> Number.  Methods added to Number belong to Number only, they aren't
> inherited by other objects.

Number in my reply above should of course have been Function, i.e.
methods added to Function.prototype are inherited by instances of
Function, which includes constructors.

As a further note, had method been added to Function, to have the same
effect it would have to have been called as:

  Function.method.call(constructor, name, func);

  e.g. from the Number example:

  Function.method.call(Number, 'integer' , function() {...});

rather than:

  constructor.method(name, func);

--
Rob


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
hubert.kau...@travelbasys.de  
View profile  
(1 user)  More options Nov 13 2008, 3:13 am
Newsgroups: comp.lang.javascript
From: hubert.kau...@travelbasys.de
Date: Thu, 13 Nov 2008 00:13:42 -0800 (PST)
Local: Thurs, Nov 13 2008 3:13 am
Subject: Re: Augmenting functions
On Nov 13, 3:28 am, disappearedng <disappeare...@gmail.com> wrote:

>        Function.prototype.method = function( name, func ) {
>            this.prototype[ name ] = func;
>            return this;
>        };
>        Number.method( 'integer', function() {
>            return Math[ this < 0 ? 'ceil' : 'floor' ]( this );
>        } );
> What the hell is Number doing?

The above code assigns a new method named 'integer' to all Number
objects.
You can test it as follows:

        var x = 3.5;
        alert( x.integer() ); // 3

        var y = -4.5;
        alert( y.integer() ); // -4

Hubert

PS: Note that 'ceil' is the correct name, not 'ceiling'.


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
John G Harris  
View profile  
 More options Nov 13 2008, 2:42 pm
Newsgroups: comp.lang.javascript
From: John G Harris <j...@nospam.demon.co.uk>
Date: Thu, 13 Nov 2008 19:42:09 +0000
Local: Thurs, Nov 13 2008 2:42 pm
Subject: Re: Augmenting functions
On Wed, 12 Nov 2008 at 18:28:42, in comp.lang.javascript, disappearedng
wrote:
>Hi everyone,
>I am reading Javascript the good parts and came across this that I
>don't understand:

  <snip>

I hope you've understood that Crockford is creating the language he
would have invented if he'd had the chance. It's not always easy to work
out why he's doing it.

  John
--
John Harris


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
dhtml  
View profile  
(2 users)  More options Nov 13 2008, 8:45 pm
Newsgroups: comp.lang.javascript
From: dhtml <dhtmlkitc...@gmail.com>
Date: Thu, 13 Nov 2008 17:45:01 -0800 (PST)
Local: Thurs, Nov 13 2008 8:45 pm
Subject: Re: Augmenting functions
On Nov 13, 11:42 am, John G Harris <j...@nospam.demon.co.uk> wrote:

> On Wed, 12 Nov 2008 at 18:28:42, in comp.lang.javascript, disappearedng
> wrote:>Hi everyone,
> >I am reading Javascript the good parts and came across this that I
> >don't understand:

>   <snip>

> I hope you've understood that Crockford is creating the language he
> would have invented if he'd had the chance. It's not always easy to work
> out why he's doing it.

I wonder why it is called 'method', when it could be called
'setPrototypeProperty'. Because that's exactly what this:

Function.prototype.method = function ( name, func ) {
    this.prototype[name] = func;
    return this;

};

- does. It's clearer to just use:-

Inline code would be an alternative:-

n |= 0; // chop off decimal.

No conversion to object, no function call and doesn't change
Number.prototype.

Garrett


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
John G Harris  
View profile  
(1 user)  More options Nov 14 2008, 3:47 pm
Newsgroups: comp.lang.javascript
From: John G Harris <j...@nospam.demon.co.uk>
Date: Fri, 14 Nov 2008 20:47:05 +0000
Local: Fri, Nov 14 2008 3:47 pm
Subject: Re: Augmenting functions

  <snip>

Because he's not a good designer ?

He sometimes forgets to type 'new' and the compiler doesn't notice, so
you mustn't use 'new'.

You might forget to type 'else' and the compiler won't notice, but
that's all right because he doesn't make that mistake.

Yea verily, he's not the thinking programmer's greatest framework
designer.

  John
--
John Harris


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google