无论在Javascript对象中调用什么函数,都调用相同的函数

时间:2015-02-27 15:16:58

标签: javascript function object

我不确定我该怎么做标题,但这是我的用例。

目前我一直在构建一些特殊对象:

test = function() {
  function _MESSAGE(func, name) {
    jQuery("#log").append(func + ' ' + name+'\n');
  }
  this.hello = {
    push: function(name) {
      _MESSAGE('hello', name);
    }
  }
  this.goodbye = {
    push: function(name) {
      _MESSAGE('goodbye', name);
    }
  }
}

var message = new test();

message.hello.push('Will'); //Returns: hello Will
message.goodbye.push('Will'); //Returns: goodbye Will
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<pre id="log"></pre>

这只是一个例子,并不完全是它的用途。

我希望能够发送消息。*。无论名称是什么,都将发送到_MESSAGE。如果您现在尝试,它会告诉您该消息。无论什么是未定义的。

我不想message('howdy', 'Will');

我希望message.howdy.push('Will')能够正常工作。

这种事情甚至可能吗?

0 个答案:

没有答案