Guerts Rick
Guerts Rick

Reputation: 43

Why doesn't my JavaScript constructor pattern work?

Why doesn't my JavaScript pattern work? For example I try to call the function this.prepare.build(), but it doesn't works. It gives me this error:

this.prepare.build is not a function

<script>
$(function () {
    new $.Myfunction();
});
</script>


<script>
(function($) {

    'use strict';
    function Myfunction(options) {
        return new Myfunction.prototype.init(options);
    }

    Myfunction.fn = $.Myfunction.prototype = {
        init: function() {
            console.log('call: Myfunction.init')
            this.prepare.build();
        },
        prepare: function() {
            return {
                 build: function() {
                        console.log('call: Myfunction.prepare.build');
                 },
                 run: function() {
                        console.log('call: Myfunction.prepare.run');
                 }
            }
        }
    }
    Inviter.prototype.init.prototype = Inviter.prototype;
})(jQuery);
</script>

Upvotes: 0

Views: 60

Answers (1)

Robert Moskal
Robert Moskal

Reputation: 22553

Might as well answer the question. prepare is a function as is build, so you need to call both:

this.prepare().build() 

Upvotes: 1

Related Questions