0

私の問題の例:

/**
 * @constructor
 */
function Marker(opts) {
    opts = opts || {};
    this.text = opts.text || 'Hello!';
    this.node = null;
    this.init();
};

Marker.prototype = {
    init: function() {
        this.node = document.createElement('div');
        this.node.innerHTML = this.text;
        document.body.appendChild(this.node);
    },
    destroy: function() {
        if ( this.node && this.node.parentNode )
            this.node.parentNode.removeChild(this.node);

        for (var i in this)
            if ( this.hasOwnProperty(i) )
                delete this[i];

        // this.constructor = null; // :-(
        // this = null; // :-(
        // H O W ?
    }
};

var first = new Marker({ text: 'first' });

alert( first instanceof Marker );
first.destroy();
alert( first instanceof Marker ); // want false

2 番目のメッセージ ボックスに false を表示するには、どのようにメソッドを更新すればよい.destroy()ですか? ソリューションは、protoを使用せずにクロスブラウジングする必要があります。

4

1 に答える 1

0

これを null に置き換える方法はありませんが、破棄されたフィールドをプロトタイプに追加し、関数の存在によって終了を確認することができます。Marker.prototype.exist=function(){ !this.destroyed; }

destroy=function(){
  ...
  this.destroyed=true;
}
于 2012-05-02T12:47:45.773 に答える