このコード内の何かを true と評価するのに問題があります。私は JavaScript に不慣れで、約 7 ~ 8 年の休止期間を経て「スクリプト作成」を始めたばかりです。その間も掘り続けます。正しい振る舞いが表現されていることを知っていれば役立つと思います。変数をプロトタイプ関数に「渡す」ことはしていないと思います。
プロトタイプ関数内に配置した alert() は、this.type の正しい値を表示します。
var imgSrc = 'pipx4.jpg';
var INITIALIZED = false;
var RUNNING = false;
var it;
function MoveableObject(h, t){
this.x = 0;
this.y = 0;
this.type = t;
this.node = 0;
this.id = h;
this.Initialize()
}
function MoveableObject.prototype.setPosition(x, y){
this.node.style.left = this.x + 'px';
this.node.style.top = this.y + 'px';
}
function MoveableObject.prototype.Initialize(){
これらの if は、alert() が正しいタイプを表示しても true と評価しません。
if(this.type == 'image'){
alert(this.id + ': MoveableObject('+this.type+'));
this.node = document.createElement('div');
this.node.innerHTML = '<img src="' + imgSrc + '" />';
document.body.appendChild(this.node);
}
if(this.type == 'text'){
alert(this.id + ': MoveableObject('+this.type+')');
this.node = document.createElement('div');
this.node.innerHTML = 'Blank';
document.body.appendChild(this.node);
}
this.node.setAttribute('id', this.id);
this.node.style.padding = '0';
this.node.style.margin = '0';
this.node.style.position = 'absolute';
}
function w(node, s){
document.getElementById(node.getAttribute('id')).innerHTML = s;
}
function INIT(){
it = new MoveableObject('square', 'text');
}
function MAIN(){
RUNNING = true;
if(!INITIALIZED){ INIT(); INITIALIZED = true;}
}
window.setInterval("MAIN()", 1000);