Skip to content

Instantly share code, notes, and snippets.

@tamzinblake
Created October 12, 2011 20:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tamzinblake/1282443 to your computer and use it in GitHub Desktop.
Save tamzinblake/1282443 to your computer and use it in GitHub Desktop.
javascript function prototype
function foo (p, v) {
if (arguments.length > 1) {
arguments.callee[p] = v
}
return arguments.callee[p]
}
function arrayFunction (arr) {
for (var i = 0; i < arr.length; i++) {
this[i] = arr[i]
}
}
arrayFunction.prototype = foo
var a = new arrayFunction([1, 3, 4])
, b = new arrayFunction([1, 3, 4])
a(1)
b(0, -1)
@tamzinblake
Copy link
Author

Note that a(1) throws a TypeError of type called_non_callable, while a.__proto__(1) is fine. That does not make sense.

@tamzinblake
Copy link
Author

Simpler example here https://gist.github.com/1282497

@isaacs
Copy link

isaacs commented Oct 12, 2011

The issue is that new always returns an object, unless the ctor explicitly returns something else. Try this:

function bar (p) { return p }
function barcons () {
  var f = function () {}
  f.__proto__ = barcons.prototype
  return f
}
barcons.prototype=bar
var c = new barcons
c(1)

Of course, this is evil and stupid, and will probably send the interpreter into some kind of crazy bad place. But you're already using arguments.callee, so clearly you're ok with evil ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment