0
例からJavaScriptでのオブジェクトの破壊:私は2番目のメッセージボックスに虚偽を見たい場合はは、右の私の問題のためのコンストラクタ
/**
* @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
どのように私は、メソッド.destroy()
を更新する必要がありますか? プロトタイプを使用せずにソリューションをクロースブラウジングする必要があります。そこ
'最初= null'なので、それはあなたの唯一のオプションです(あるいは少なくともそれが可能この文脈では最も意味がある)。 –
最初の値に別の値を代入する以外に、プロトタイプでのマングリングを含まないオプションはありません。[ECMAScript](http://es5.github.com/#x15.3.5.3)では 'instanceof'に依存していますプロトタイプ比較。 –