JavaScript One-liner to change a prototype method into a function in JavaScript

The String object in Javascript has a method called toLowerCase. The problem with that method is that it requires that you already have a String object.

What if I wanted to turn that prototype method into a function that could be called on a String?

I know that I can do this:

var f = function (str) { return str.toLowerCase(); }
f("HI") // = "hi"

I want to avoid using the function keyword.

I thought that, since I can do this...

String.prototype.toLowerCase.call("HI") // = "hi"

...that I could then do this:

var f = String.prototype.toLowerCase.call
f("HI")

But this results in an error:

Uncaught TypeError: f is not a function

Same goes for apply and bind.

I want the resulting function to be callable using f("HI"), so using call and apply aren't allowed.

Is there way to accomplish this without resorting to using function?

For those of you who will inevitably wonder why I won't use the function keyword, it's just curiosity to help me understand JavaScript better. I'm not trying to be fancy, or efficient, or anything. I'm not trying to optimize prematurely, etc., so please refrain from Knuth quotes.

Answer:1

Got it. Quite a mental puzzle you put together, and I wasn't sure there was any way to have the first argument be accepted how we want it.

var pointlessFunctionBindingForJavascriptHackers =
   Function.prototype.call.bind(String.prototype.toLowerCase)
Answer:2

Please find this jsfiddle I've created drag n drop no sorting The problem here is that when I put the box from drag-gable area to "drop here" area, the boxes won't be sorted out. For example If I put ...

Please find this jsfiddle I've created drag n drop no sorting The problem here is that when I put the box from drag-gable area to "drop here" area, the boxes won't be sorted out. For example If I put ...

  1. jquery drag drop file upload
  2. jquery drag drop list
  3. jquery drag drop table rows
  4. jquery drag drop example
  5. jquery drag drop file
  6. jquery drag drop image
  7. jquery drag drop event
  8. jquery drag drop plugin
  9. jquery drag drop upload
  10. jquery drag drop order
  11. jquery drag drop clone
  12. jquery drag drop get dropped element
  13. jquery drag drop
  14. jquery drag drop sort
  15. jquery drag drop table
  16. jquery drag drop mobile
  17. jquery drag drop grid
  18. jquery drag drop sortable
  19. jquery drag drop image upload
  20. jquery drag drop list items

I have some replaying functionality working by using Javascript and the window.onload function. However when I go to some other page and then come back to the web application (with the onload ...

I have some replaying functionality working by using Javascript and the window.onload function. However when I go to some other page and then come back to the web application (with the onload ...

when i wrote in my "npm cache clean" this error is occurring "npm ERR! Windows_NT 6.3.9600 npm ERR! argv C:\iaAC>npm cache clean npm ERR! Windows_NT 6.3.9600 npm ERR! argv "C:\\Program Files\\...

when i wrote in my "npm cache clean" this error is occurring "npm ERR! Windows_NT 6.3.9600 npm ERR! argv C:\iaAC>npm cache clean npm ERR! Windows_NT 6.3.9600 npm ERR! argv "C:\\Program Files\\...

I'm populating my CodeMirror textarea with an unformatted string of CSS. I have no control over the formatting of this CSS. Unformatted CSS example position: absolute; top: 66px; left: 746px; width: ...

I'm populating my CodeMirror textarea with an unformatted string of CSS. I have no control over the formatting of this CSS. Unformatted CSS example position: absolute; top: 66px; left: 746px; width: ...

  1. codemirror auto format
  2. codemirror auto format code
  3. codemirror auto format json
  4. codemirror auto format all
  5. codemirror auto format xml
  6. codemirror auto indent