That's quite odd actually, on the bus from London to Oxford this morning I was
wondering if they should be named this way in Prototype. But, yes, I have been
doing it this way for quite some time in my own projects. I think it is much
more intuitive.
I would probably use Function.IDENTITY rather than Function.K because probably
more people are familiar with 'the identity function' than 'k'.
--
Jim
my wiki ajaxification thing: http://wikizzle.org
my blog: http://jimhigson.blogspot.com/
I'd say returnFn sounds like a function which returns a function.
On Sat, 26 Sep 2009 07:04:22 +0400, kangax <kan...@gmail.com> wrote:
> onselectstart
I have Function.alwaysReturn( val );
So, I'd use Function.alwaysReturn( false ) in that case.
Is good for anywhere a function is expected but you want to insert a value.
Eg, Function.alwaysReturn( 4 );
Function.alwaysReturn =
function ( val ){
return (function(){return val});
};
Well, it *could* do:
Function.empty = function(){};
Function.empty.empty = function(){return true};
Of course this doesn't cover other empty functions and I may be not entirely
serious ;-)
Jim
First, I'm fine with calling it Function.IDENTITY instead of Function.K.
On Sep 25, 2009, at 1:26 AM, T.J. Crowder wrote:
> Make sense. Shouldn't that be Function.empty and Function.k, though,
> to follow our naming rules? And perhaps Function.k should have a more
> meaningful name.
I'm very much in favor of all-caps, or at least initial caps, to
indicate that these are constants. We don't do this consistently in
the framework so far, but I'd like it to match stuff like
Event.KEY_ESC. I think the capitalization will also help distinguish
it from String#empty and Array#empty, as was mentioned elsewhere.
(Aside: I'm also in favor of renaming those methods to `isEmpty` for
2.0.)
What do people think about the capitalization?
Cheers,
Andrew
> Don't get me wrong, but we are not talking about splitting the core
> of the atom in 16 different particles. Instead the topic of
> discussion is how to name (and where to place) the empty and
> identity function.
>
> Two things should be taken into account -- common sense and
> consistency.
>
> Deriving from mathematics, how is this function called -- f(x) = x,
> for every instance of x? Identity function. That's a the perfect
> name (as mentioned above).
>
> And since this is not a constant function, no need to be
> capitalized. And since JS is generally camel case, first letter
> stays lowercase.
I beg to differ. IDENTITY is constant in function value space
similarly as PI is constant in floating point value space. There is
only one IDENTITY and only one PI...
This calls for all-upper-case, similarly for K (or whatever it is
called)...
izidor
> Or to put it another way, what is the difference between "f(x) = x"
> and "f(x) = x+1"... why would the former be up-cased, but not the
> latter?
Ours already has a name in mathematics - identity function, and it is
primarily used by assigning its value (like var f =
Function.IDENTITY), not by calling it (like Function.IDENTITY(3); )
We want this value to be assigned or passed as parameter. That is the
main purpose of the definition. Our interest in other functions is
primarily in calling them - you rarely assign Math.random.
So the difference is in usage - is it used more as value or more as
function call ? If it is value, then it is treated as constant (which
it is). If it is called, it is treated as function name.
IDENTITY is obviously never meant to be called explicitly, so it is a
constant value meant to be passed around.
izidor