John Resig の「 Pro Javascript Techniques 」を読んでいて、例と混同しています。これはコードです:
// Create a new user object that accepts an object of properties
function User( properties ) {
// Iterate through the properties of the object, and make sure
// that it's properly scoped (as discussed previously)
for ( var i in properties ) { (function(){
// Create a new getter for the property
this[ "get" + i ] = function() {
return properties[i];
};
// Create a new setter for the property
this[ "set" + i ] = function(val) {
properties[i] = val;
};
})(); }
}
// Create a new user object instance and pass in an object of
// properties to seed it with
var user = new User({
name: "Bob",
age: 44
});
// Just note that the name property does not exist, as it's private
// within the properties object
alert( user.name == null );
// However, we're able to access its value using the new getname()
// method, that was dynamically generated
alert( user.getname() == "Bob" );
// Finally, we can see that it's possible to set and get the age using
// the newly generated functions
user.setage( 22 );
alert( user.getage() == 22 );
これを Firebug コンソール (FF3) で実行すると、user.getname() が関数ではないことがスローされます。私はこれをやってみました:
var other = User
other()
window.getname() --> this works!
そしてそれは働いた!
理由はありますか?みんなありがとう!
PS: この本を強くお勧めします。
編集:
やっている:
var me = this;
少しうまくいくようですが、「getname()」を実行すると「44」(2 番目のプロパティ) が返されます...
また、変更せずにウィンドウオブジェクトで機能したのも奇妙だと思います...
3 番目の質問は、PEZ ソリューションとオリジナルの違いは何ですか? (彼は無名関数を使用しません)
フィードバックをお寄せいただきありがとうございます。+1