Skip to content Skip to sidebar Skip to footer

JS Run Function From Constructor After Object Instantiation

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

Solution 1:

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.


Post a Comment for "JS Run Function From Constructor After Object Instantiation"