Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use of the identity function in JavaScript

I use the identity function in all my JavaScript programs:

function identity(value) {
    return value;
}

The reason is that I often need differentiate between primitives types (undefined, null, boolean, number and string) and object types (object and function) as returned by the typeof operator. I feel using the indentity function for this use case very succuint:

if (new identity(value) == value); // value is of an object type
if (new identity(value) != value); // value is of a primitive type

The identity function is much smaller and simpler than the following code:

function isObject(value) {
    var type = typeof value;
    return type == "object" || type == "function";
}

However on reading my code a friend of mine complained that my hack is misleading and more computationally expensive than the above alternative.

I don't want to remove this function from any of my programs as I believe it's an elegant hack. Then again I don't write programs solely for myself. Is there any other use case for the identity function in JavaScript?

like image 650
Aadit M Shah Avatar asked Jul 14 '12 16:07

Aadit M Shah


People also ask

What is the use of identity function?

The identity function is a function which returns the same value, which was used as its argument. It is also called an identity relation or identity map or identity transformation. If f is a function, then identity relation for argument x is represented as f(x) = x, for all values of x.

What is identity function JavaScript?

An Identity function is a function that does nothing. It just returns what it receives. It's like the number zero, it's just there to fill the place without doing anything, and sometimes that is exactly what is needed. So let's try to write an identity function in JavaScript.

What is an identity function programming?

An identity function, in the context of the code in your question, is simply a function that returns the same argument passed to it : In math you can denote it as: f(x) = x.

Why do we use JavaScript functions?

JavaScript provides functions similar to most of the scripting and programming languages. In JavaScript, a function allows you to define a block of code, give it a name and then execute it as many times as you want.


2 Answers

IMHO:

new identity(value) == value

means absolutely nothing and without extra comment I would have to think for a while to figure out what the intent was. On the other hand:

isObject(value)

is obvious from the very beginning, no matter how it is implemented. Why can't you use your hack inside a function named isObject()?

BTW More suited for http://codereview.stackexchange.com.

like image 160
Tomasz Nurkiewicz Avatar answered Sep 28 '22 11:09

Tomasz Nurkiewicz


I updated my "speedtest" to test if the right results are returned … they aren't:

If you compare with new identity(x) == x, then null is deemed an object. === works, though.

Such pitfalls speak in favor of the isObject(...) solution.

If you compare === 'object'/'function' in the isObject code, then it will be double as fast as your original implementation, and almost a third faster than new identity(x) === x.

like image 31
kay Avatar answered Sep 28 '22 10:09

kay