http://jsfiddle.net/QWXf4/1/の JavaScript コード スニペットを次に示します。
var x = 5;
function PartyWithoutX(person) {
// This property will go to window and pollute the global object
dance = function (person) {
document.write("Hello " + person.getFullName() + ", Welcome to the Party.");
};
this.showX = function () {
// This will change the global x variable
x = 25;
document.write("Value of x is " + x);
};
}
function Person(firstName, lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
Person.prototype.getFullName = function () {
return this.firstName + " " + this.lastName;
};
var dinesh = new Person("Dinesh", "Singh");
var p1 = new PartyWithoutX(dinesh);
document.write(window.dance(dinesh) + "; Enjoy!!");
document.write("<br/>");
document.write(p1.showX());
document.write("<br/>");
document.write(window.x);
document.write("<br/>");
確認できる出力は次のとおりです
Hello Dinesh Singh, Welcome to the Party.undefined; Enjoy!!
Value of x is 25undefined
undefined
私は期待していました
Hello Dinesh Singh, Welcome to the Party; Enjoy!!
Value of x is 25
undefined
出力に「Party.undefined」と「25undefined」が表示されるのはなぜですか。
何が起きてる?