JS: TypeError: this._init is not a function. (In 'this._init()', 'this._init' is undefined)
JS: TypeError: this._init is not a function. (In 'this._init()', 'this._init' is undefined)
也许你会发现问题所在。控制台总是说:
TypeError: this._init 不是函数。 (在'this._init()'中,'this._init'未定义)
nodes = [];
for (var i = 0; i < 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");
}
}
看起来你在定义它之前调用了 _init。
this._init = function() {
alert('test');
}
this._init();
您需要在调用之前定义函数:
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();
}
如果您在别处定义函数之前调用过函数,您可能会对此感到困惑。在某些情况下,您的功能可能是 "hoisted"
到你的脚本的顶部。下面是一个完全合法的调用:
isItHoisted();
function isItHoisted() {
console.log("Yes!");
}
http://adripofjavascript.com/blog/drips/variable-and-function-hoisting
您现在可能已经知道,对象上的方法函数没有提升,所以您会看到您看到的错误。
也许你会发现问题所在。控制台总是说:
TypeError: this._init 不是函数。 (在'this._init()'中,'this._init'未定义)
nodes = [];
for (var i = 0; i < 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");
}
}
看起来你在定义它之前调用了 _init。
this._init = function() {
alert('test');
}
this._init();
您需要在调用之前定义函数:
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();
}
如果您在别处定义函数之前调用过函数,您可能会对此感到困惑。在某些情况下,您的功能可能是 "hoisted" 到你的脚本的顶部。下面是一个完全合法的调用:
isItHoisted();
function isItHoisted() {
console.log("Yes!");
}
http://adripofjavascript.com/blog/drips/variable-and-function-hoisting
您现在可能已经知道,对象上的方法函数没有提升,所以您会看到您看到的错误。