0

I'm not exactly sure what I should Title this, but here's my use case.

Currently I've been building some special objects like so:

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>

This is just an example though, and not exactly what it's being used for.

I would like to be able to make message.*.push send to _MESSAGE no matter what the name is. If you'd try that now, it would tell you that message.whatever is undefined.

I don't want to do message('howdy', 'Will');

I want message.howdy.push('Will') to work.

Is this type of thing even possible?

ethree
  • 1,582
  • 2
  • 14
  • 21

0 Answers0