Prototype property for a function

Viewed 74

I'm trying to get this:

a = new Foo(name); // a is function
a(); // returns a result
a.set(); // returns another result

I've implemented above like that:

function Foo (name) = {
  val = name;
  ret = function () { return val; }
  ret.set = function (v) { return val = v; }
  return ret;
}

Then, for multiple instances of Foo I'd like not to create method 'set', but share it through prototype property. However, all experiments I did have no effect. It works only on objects, not on functions. Even the code below doesn't work:

foo = function () { return 'a'; }
foo.foo = function ()  { return 'foo'; }
foo.prototype.bar = function ()  { return 'bar'; }

foo(); // a
foo.foo(); // foo
foo.bar(); // Uncaught TypeError: Object function () { return 'a'; } has no method 'bar' 
3 Answers
Related