0.0.2 • Published 10 years ago

redefine-function v0.0.2

Weekly downloads
2
License
MIT
Repository
-
Last release
10 years ago

Redefine Function

Re-define functions

Install

$ npm install --save redefine-function 

Usage

var redefineFunction= require("redefine-function")

var Greetings = {
  hello: function(){
    return "Hi there."
  }
}

console.log(Greetings.hello()) // Hi there

redefineFunction(this, "Greetings.hello", function(){
  return  "Don't worry about it"
})

console.log(Greetings.hello()) // Don't worry about it

API

redefineFunction(context, functionName, newFunction)

NameTypeDescription
ContextObjectThe context of the object or function you want to redefine
functionNameStringA string representation of the function you want to redefine
newFunctionFunctionThe function you want to override with
var redefineFunction= require("redefine-function")

var Greetings = {
  hello: function(){
    return "Hi there."
  }
}

console.log(Greetings.hello()) // Hi there

redefineFunction(this, "Greetings.hello", function(){
  return  "Don't worry about it"
})

console.log(Greetings.hello()) // Don't worry about it
Also works with prototype functions:
var redefineFunction= require("redefine-function")

String.prototype.yell = function(){
  return this + "!"
}

function reallyYell(){
  return this + "!!!!!!!!"
}

"Hello".yell() // #=>  "Hello!"

redefineFunction(global, "String.prototype.yell", reallyYell)

"Hello".yell() // #=>  "Hello!!!!!!!!"