0

私は友人と私が問題を抱えていたこのエラーを取り除こうとしています。エラーはタイトルにあり、93行目で発生しています...アイデアや提案はありますか? 行 93 は、以下のコメントでマークされています。document.body.innerHTML.replace("__ITEM__", pocket.item_name); //LINE 93

私が言及したいもう1つのことは、不要なコードをすべて切り取ったことです(私は思う)ので、別のセクションが必要かどうか尋ねてください.

これが初心者の間違いであっても驚かないので、お気軽に声をかけてください。また、あなたが見つけた悪い習慣やそのようなものについてもお詫び申し上げます。私はまだこれに慣れていません。

関数start()が最初に呼び出されます。

var status, items_none, items, pocket, money;

function item(item_name, usage_id, description, minimum_cost) {
    this.item_name = item_name;
    this.usage_id = usage_id;
    this.description = description;
    this.worth = minimum_cost;
    this.usage_verb = "Use";
    this.choose_number = false;
}    
function start() {
    status = "Welcome to Collector.";

    items_none = item("---", -2, "Your pockets are empty.", 0);
    items = new Array();
    items[0] = item("Cardboard Box", 0, "Open the box to see what's inside.", 100);
    ...

    pocket = items_none; //Start with empty pockets.
    money = 100; //Start with 0 coins.

    updateGui();
}
function updateGui() {
    //This updates all text on the page.
    document.body.innerHTML.replace("__COINS__", money);
    document.body.innerHTML.replace("__ITEM__", pocket.item_name); //LINE 93
    document.body.innerHTML.replace("__STATUS__", status);
    document.body.innerHTML.replace("__ITEM:USE__", pocket.usage_verb);
    document.body.innerHTML.replace("__ITEM:DESC__", pocket.description);
    document.body.innerHTML.replace("__ITEM:WORTH__", pocket.worth);
    document.body.innerHTML.replace("__ITEM:VERB__", pocket.usage_verb);
}

いつものように、事前に感謝し、コーディングをお楽しみください!

4

1 に答える 1

3

毎回new前に追加します。item

items_none = new item("---", -2, "Your pockets are empty.", 0);
...
items[0] = new item("Cardboard Box", 0, "Open the box to see what's inside.", 100);

どうしてこれなの?と呼ばれる関数を考えてみましょうpair:

function pair(x, y) { this.x = x; this.y = y; }

new単純な関数呼び出しを行っていることを意味せずに呼び出すだけです。this現在のオブジェクト コンテキストを参照するだけで、おそらくwindow.

p = pair(55, 66);
alert(window.x == 55); // true!
alert(p.x); // error--p is undefined.

'new' は関数を期待し、それをコンストラクターとして扱います。this新しいオブジェクトに設定されます。

p = new pair(55, 66);
alert(window.x == 55); // false!
alert(p.x); // 55!
于 2013-10-20T01:49:00.850 に答える