私はいくつかの情報を持つオブジェクトを持っています。一方、私は、このオブジェクト型の多くのオブジェクトを保持しているストアを持っています。Javascriptオブジェクト自体をパラメータとして渡す
単一のオブジェクトは、自分自身をこのストアから削除できる必要があります。 私の店は、このオブジェクトのパラメータを持つ関数 "DeleteObjectFromStore"を知っています。 これでオブジェクト自体を渡したいと思いますが、使用する構文がわかりません。
店舗:
class NoteStore {
constructor() {
this.notes = []; // Collection of all notes
}
DeleteNote(note) { // Delete this object from the store
var index = this.notes.indexOf(note);
if (index > -1)
array.splice(index, 1);
}
}
そして、私のオブジェクトだから、これは正しくないキーワードを使用して
class Note {
constructor(noteTitle, noteText, noteStore) {
this.title = noteTitle; // name
this.text = noteText; // content
this.store = noteStore; // dataStore
}
CreateDeleteButton(parentDiv) { // Create a button for deleting itself
var data = this.store;
var deleteBtn = document.createElement("input");
deleteBtn.type = "button";
deleteBtn.value = "Delete";
deleteBtn.onclick = function() {
data.DeleteNote(); // <= missing parameter!
}
parentDiv.appendChild(deleteBtn);
}
}
。それから私はボタンを渡します。
誰かが私を助けてくれますか?
あなたはthis.notes' 'から' Note'インスタンスを削除しようとしていますか? 'array'はどこに定義されていますか? – guest271314