私は JavaScript に関する本を読んでいました (最近学び始めました)。本の例の 1 つを実行すると、エラーが発生します。Ubuntu 14.0.835.202 で Chromium ブラウザーを使用しています。
私は初心者なので、なぜエラーが発生するのか理解できません。前もって感謝します。
Function.prototype.method = function (name, fn)
{
this.prototype[name] = fn;
return this;
};
var Person
{
this.name = name;
this.age = age;
};
Person.
method ("getName", function
{ // error here - "Uncaught SyntaxError: Unexpected token {"
return this.name;
}).
method ("getAge", function
{
return this.age;
});
var alice = new Person ("Alice", 93);
var bill = new Person ("Bill", 30);
Person.
method ("getGreeting", function
{
return "Hi" + this.getName() + "!";
});
alert (alice.getGreeting());
編集:
解決策は、私が尋ねたかった別の質問を私に与えました. オブジェクト宣言の場合:
var Object = function (...) // line 1
{
// code here
};
変数の数が多すぎて 1 行目にリストしたくない場合は、どうすればよいですか?