1

I am having trouble getting something in this code to evaluate as true. I am newish to JavaScript and have only begun 'scripting' after about 7-8 years hiatus. Ill keep digging in the meantime. I think it would help if I knew the correct behavior being expressed. I think im not 'passing' a variable 'into' the prototype function.

The alert()'s I have placed inside the prototype function display the correct value of 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(){

These if's do not evaluate as true even though the alert() displays the correct type.

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);
4

0 回答 0