jquery - Javascript TypeError: this.init is not a function Error - Stack Overflow

This is my Javascript codeHtml5Template_300x250 = function(config) {this.config = config;var self = thi

This is my Javascript code

    Html5Template_300x250 = function(config) {
       this.config = config;
        var self = this;
        this.init();       
        Html5Template_300x250.prototype = {
            // Function That Creates Element Var
            d: function(id) {
                return document.getElementById(id);
            },

            // Initialize DCO HTML5 template
            init: function() {
                alert("test1");
                this.startAd();

            },

            startAd: function() {
                alert("test2");

            }

        };
    }

From the HTML file i am creating method like this

 var sr_Config = {            
            bgColor:'#fff',
            ctaText:'Learn More',
            border: 'true'
        };


        var Html5Template = new Html5Template_300x250(sr_Config);

But i am getting Error

TypeError: this.init is not a function this.init();

I am not sure what is wrong here i have also tried self.init() but still it is not working.

I am new to javascript and learning OOPS in Javascript if anyone can tell me what i am doing wrong here that would be great. Thanks in advance

This is my Javascript code

    Html5Template_300x250 = function(config) {
       this.config = config;
        var self = this;
        this.init();       
        Html5Template_300x250.prototype = {
            // Function That Creates Element Var
            d: function(id) {
                return document.getElementById(id);
            },

            // Initialize DCO HTML5 template
            init: function() {
                alert("test1");
                this.startAd();

            },

            startAd: function() {
                alert("test2");

            }

        };
    }

From the HTML file i am creating method like this

 var sr_Config = {            
            bgColor:'#fff',
            ctaText:'Learn More',
            border: 'true'
        };


        var Html5Template = new Html5Template_300x250(sr_Config);

But i am getting Error

TypeError: this.init is not a function this.init();

I am not sure what is wrong here i have also tried self.init() but still it is not working.

I am new to javascript and learning OOPS in Javascript if anyone can tell me what i am doing wrong here that would be great. Thanks in advance

Share edited Aug 13, 2014 at 7:02 NoobEditor 15.9k20 gold badges85 silver badges117 bronze badges asked Aug 13, 2014 at 6:59 user3754380user3754380 7112 gold badges7 silver badges15 bronze badges 1
  • 1 you should define the init method before you use/call it. (so write the call to init below the init function itselfs) – roel Commented Aug 13, 2014 at 7:16
Add a ment  | 

1 Answer 1

Reset to default 3

You need to assing the methods to the prototypes properties (at least thats how i do it). You also need to do so before you call the function (above).

Html5Template_300x250 = function(config) {
    this.config = config;
    var self = this;

    Html5Template_300x250.prototype.d = function(id) {
        return document.getElementById(id);
    };

    Html5Template_300x250.prototype.startAd = function() {
        alert("test2");
    };

    // Initialize DCO HTML5 template
    Html5Template_300x250.prototype.init = function() {
        alert("test1");
        this.startAd();
    };

    self.init();
}

Another way to do this w/o the prototype-stuff would be sth. like that:

Html5Template_300x250 = function(config) {
    this.config = config;
    var self = this;

    this.d = function(id) {
        return document.getElementById(id);
    };

    // and so on..

    self.d('myid');
}

See this working fiddle with some sample code.

Further interesting reading on the topic OOP in JS is provided by JS-Guru Douglas Crocford ;)

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742403996a4437474.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信