如何彻底删除

时间:2013-12-31 13:13:24

标签: javascript design-patterns

请帮助删除新的Bullet。 jsfiddle

在物体从底部向上移动穿过屏幕后。与y< = 5px方法协调工作helper.deleteElement(),但对象仍然存在(参见console - console.log('move');)

我需要完全删除

JS:

Bullet = function(bulletId){ 
    var self = this;

    self.id = bulletId; 
    self.x = 100;   
    self.y = 500;   

    self.move = function(){ 
        console.log('move');

        var offsetY = -10,
            newY = self.y + offsetY;

        if(newY <= 5){
            helper.deleteElement(self.id);

            if(observerable.removeListener(this, "makeMoveBullet", "move")){
                console.log('dont worked!');
            }
        }   

        self.y = newY;

        self.render(self.x, newY);      

        return;
    };      

    self.render = function(x, y){       
        $('#' +self.id).css({
            'left': (self.x = x || self.x) + 'px',
            'top': (self.y = y || self.y) + 'px'
        }).appendTo('#wrap');   

        return;
    };

    self.init = function(playerX, playerY){
        $('<div class="bullet" id="' + self.id + '"  />').appendTo('#wrap');

        self.x = playerX + 31;
        self.y = playerY - 9;

        self.render(self.x, self.y);

        return;
    };
}; 


// ------------------------------------------------------------------------------------ observer
var observerable = {
    listeners: {},

    addListener: function (object, evt, callback) {       
        if (!this.listeners.hasOwnProperty(evt)) {           
            this.listeners[evt] = [];    
        }        

        this.listeners[evt].push(object[callback]);       
    },

    removeListener: function (object, evt, callback) {       
        if (this.listeners.hasOwnProperty(evt)) {      
            var i, length;     
                    
            for (i = 0, length = this.listeners[evt].length; i < length; i += 1) {               
                if (this.listeners[evt][i] === object[callback]) {                   
                    this.listeners[evt].splice(i, 1);        
                }      
            }    
        }  
    },
        
    publisher: function (evt, args) {       
        if (this.listeners.hasOwnProperty(evt)){                       
            var i, length;       
                                      
            for (i = 0, length = this.listeners[evt].length; i < length; i += 1){                
                this.listeners[evt][i](args);      
            }            
        }                            
    }
}; 

// ------------------------------------------------------------------------------------ helper
function Helper(){
    this.deleteElement = function(id){
        $('#' + id).remove();

        return;
    };  
};

// ------------------------------------------------------------------------------------ init
var helper = new Helper();


bullet = new Bullet('bullet_0'); 

bullet.init(500, 500);


observerable.addListener(bullet, "makeMoveBullet", "move"); 

setInterval(function(){
    observerable.publisher("makeMoveBullet");
}, 100);

3 个答案:

答案 0 :(得分:1)

JavaScript使用垃圾收集器删除未使用的变量。我会说要做这样的事情:

delete this.x; delete this.y ; ... ; delete this;

当您想要删除对象时(也将其从任何其他外部数据结构中删除)。删除它的所有属性然后删除对象本身。然后GC应该删除它。

答案 1 :(得分:0)

setInterval返回间隔标识符。当您不希望更多地执行间隔时,使用clearInterval函数清除间隔。

var int = setInterval(...);
...
clearInterval(int);

答案 2 :(得分:0)

removeListener: function (object, evt, callback) {
    if (this.listeners.hasOwnProperty(evt)) {
        var i, length;

        for (i = 0, length = this.listeners[evt].length; i < length; i += 1) {
            if (this.listeners[evt][i] === object[callback]) {
                this.listeners[evt].splice(i, 1);
            }
        }
    }
}

object[callback]始终未定义,因此this.listeners[evt].splice(i, 1);无法执行;