Function

Prototype takes issue with only one aspect of functions: binding.

What is binding?

“Binding” basically determines the meaning, when a function runs, of the this keyword. While there usually is a proper default binding (this refers to whichever object the method is called on), this can be “lost” sometimes, for instance when passing a function reference as an argument.

If you don’t know much about the this keyword in JavaScript, hop to the docs for the bind() method. The examples there will clear it up.

Prototype to the rescue!

Prototype solves this. You’ll find two new methods on any function: one that guarantees binding (it can even guarantee early parameters!), and one that is specific to functions intended as event handlers.

Methods

argumentNames
1.6

someFunction.argumentNames() -> Array

Reads the argument names as defined in the function definition and returns the values as an array of strings, or an empty array if the function is defined without parameters.

bind

bind(thisObj[, arg...]) -> Function

Wraps the function in another, locking its execution scope to an object specified by thisObj.

bindAsEventListener

bindAsEventListener(thisObj[, arg...]) -> Function

An event-specific variant of bind which makes sure the function will recieve the current event object as the first argument when executing.

curry
1.6.0


  curry(arg...) -> Function

Partially applies the function, returning a function with one or more arguments already “filled in.”

defer
1.6.0

defer(arg...) -> Number

Schedules the function to run as soon as the interpreter is idle.

delay
1.6.0

delay(seconds[, arg...]) -> Number

Schedules the function to run after the specified amount of time, passing any arguments given.

methodize

someFunction.methodize() -> Function

Takes a function and wraps it in another function that, at call time, pushes this to the original function as the first argument.

wrap
1.6.0

wrap(wrapperFunction[, arg...]) -> Function

Returns a function “wrapped” around the original function.