Created
February 24, 2010 19:47
-
-
Save furf/313773 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// punch | |
jQuery.punch = function (obj, method, fn) { | |
var hasOwnMethod = obj.hasOwnProperty(method), | |
original = obj[method]; | |
obj[method] = function () { | |
Array.prototype.unshift.call(arguments, $.proxy(original, this)); | |
return fn.apply(this, arguments); | |
}; | |
$.data(obj[method], '(*)<', hasOwnMethod && original); | |
return obj[method]; | |
}; | |
// pull | |
jQuery.pull = function (obj, method) { | |
var original = $.data(obj[method], '(*)<'); | |
$.removeData(obj[method], '(*)<'); | |
if (original) { | |
obj[method] = original; | |
} else { | |
obj[method] = null; | |
delete obj[method]; | |
} | |
return obj[method]; | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// view results at http://furf.com/exp/duck/duck.html | |
(function ($) { | |
function Person (name) { | |
this.name = name; | |
} | |
Person.prototype.doSomething = function () { | |
console.log(this.name + ' is doing something'); | |
}; | |
var d = new Person ('dave'); | |
d.doSomethingElse = function () { | |
console.log(this.name + ' is doing something else'); | |
}; | |
console.group('prototype'); | |
console.group('punch'); | |
$.punch(Person.prototype, 'doSomething', function (orig) { | |
console.log('punched!'); | |
orig(); | |
}); | |
d.doSomething(); | |
console.groupEnd('punch'); | |
console.group('pull'); | |
$.pull(Person.prototype, 'doSomething'); | |
d.doSomething(); | |
console.groupEnd('pull'); | |
console.groupEnd('prototype'); | |
console.group('instance'); | |
console.group('punch'); | |
$.punch(d, 'doSomething', function (orig) { | |
console.log('punched!'); | |
orig(); | |
}); | |
d.doSomething(); | |
console.groupEnd('punch'); | |
console.group('pull'); | |
$.pull(d, 'doSomething'); | |
d.doSomething(); | |
console.groupEnd('pull'); | |
console.groupEnd('instance'); | |
console.log('weird things start to happen down here...'); | |
console.log('look into mapping at actual prototype if !hasOwnMethod but avoid infinite recursion'); | |
console.group('prototype & instance'); | |
console.group('punch'); | |
$.punch(Person.prototype, 'doSomething', function (orig) { | |
console.log('punched! (prototype)'); | |
orig(); | |
}); | |
d.doSomething(); | |
console.groupEnd('punch'); | |
console.group('punch'); | |
$.punch(d, 'doSomething', function (orig) { | |
console.log('punched! (instance)'); | |
orig(); | |
}); | |
d.doSomething(); | |
console.groupEnd('punch'); | |
console.group('pull'); | |
$.pull(Person.prototype, 'doSomething'); | |
d.doSomething(); | |
console.groupEnd('pull'); | |
console.groupEnd('prototype & instance'); | |
})(jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment