Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

fixes for .call() and .apply()

  • Loading branch information...
commit 98acde171bc26c56e5f6a1429d282a74de0025e6 1 parent dc11364
@substack authored
Showing with 17 additions and 3 deletions.
  1. +15 −1 index.js
  2. +1 −1  package.json
  3. +1 −1  test/foo.js
View
16 index.js
@@ -3,7 +3,21 @@ module.exports = function (obj, fn) {
if (typeof fn !== 'function') return;
return fn.apply(obj, arguments);
};
- f.__proto__ = obj.__proto__;
+
+ function C () {}
+ C.prototype = Object.getPrototypeOf(obj);
+ f.__proto__ = new C;
+
+ if (f.__proto__.call === undefined) {
+ f.__proto__.call = function (inst) {
+ return fn.apply(inst, [].slice.call(arguments, 1));
+ };
+ }
+ if (f.__proto__.apply === undefined) {
+ f.__proto__.apply = function (inst, args) {
+ return fn.apply(inst, args);
+ };
+ }
Object.getOwnPropertyNames(obj).forEach(function (key) {
f[key] = obj[key];
View
2  package.json
@@ -1,6 +1,6 @@
{
"name" : "funstance",
- "version" : "0.0.0",
+ "version" : "0.0.1",
"description" : "make an instance callable like a function",
"main" : "index.js",
"bin" : {},
View
2  test/foo.js
@@ -16,7 +16,7 @@ Foo.prototype.beep = function () {
};
test('funstantiate foo', function (t) {
- t.plan(2);
+ t.plan(6);
var obj = new Foo(4);
var fobj = funstance(obj, function (n) {
Please sign in to comment.
Something went wrong with that request. Please try again.