Is it possible to do this:
var hammer = new Hammer(); // create a new instance
hammer(nail); // really call Hammer.prototoype.hit(object);
I can figure it out on a raw object, but not when creating a new instance of an object. This is what I am running into:
function Hammer(options) {
  this.config = options.blah;
  this.hit(/* ? */);
  return this;
}
Hammer.prototype.hit = function(obj) {
  // ...
}
When I call the constructor, I want to pass in special options - not what nail to hit. However, when I call it later, I want to pass in a nail. I'm missing something.
One solution is to not create a constructor function at all:
var hammer = newHammer();
hammer(nail);
hammer.clean();
function newHammer(options) {
    var config = options.blah;
    hit.clean = clean;
    return hit;
    function hit(obj) {
        // ...
    }
    function clean() {
        // ...
    }
}
To me, this is a much cleaner solution than messing around with constructors and prototypes.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With