javascript - JS: TypeError: this._init is not a function. (In 'this._init()', 'this._init' is undefined) -


maybe find problem. console says:

typeerror: this._init not function. (in 'this._init()', 'this._init' undefined)

nodes = []; (var = 0; < 3; i++) {     var newnode = new node(i*100,0);     nodes.push(newnode); };  function node(posx, posy, parent) {     if (typeof parent === 'undefined') { parent = 0; }     this.parent = parent;     this.children = [];     this.text = "node";     this.posx = posx;     this.posy = posy;     this._init();      this._init = function() {         alert("test");     } } 

you need define function before invoking it:

function node(posx, posy, parent) {     if (typeof parent === 'undefined') { parent = 0; }     this.parent = parent;     this.children = [];     this.text = "node";     this.posx = posx;     this.posy = posy;      this._init = function() {         alert("test");     }      this._init();    } 

http://jsfiddle.net/4wslhd8y/

you may confused if have invoked functions before defined elsewhere. under conditions, function "hoisted" top of script. following shows legal invocation:

isithoisted();  function isithoisted() {     console.log("yes!"); } 

http://adripofjavascript.com/blog/drips/variable-and-function-hoisting

as aware, method functions on objects aren't hoisted, error seeing.


Comments