开发者

Is it possible to make an object that acts like a function when called in Javascript?

I want to make an object that has properties but can also be called. Sort of like 开发者_JAVA技巧the toString property that returns a string that represents the object when it's used as a string.

something that works like this:

o = {
  prop: 2
  toFunction: function(a) {
    return a;
  }
}

o('foo'); // returns foo
o.prop; // returns 2


Function is an object in JavaScript so you can do:

var o = function(a) { return a; }
o.prop = 2;

and so

o('foo'); // returns foo
o.prop; // returns 2
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜