Created
September 3, 2011 12:34
-
-
Save ishiduca/1191122 to your computer and use it in GitHub Desktop.
教材
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// new 演算子を使って複数のオブジェクトを作った場合、 | |
// 同一のオブジェクトをプロトタイプオブジェクトにもつため、 | |
// プロトタイプオブジェクトのプロパティをいじった場合、 | |
// 他のオブジェクトのプロパティも変わる危険性がある | |
// 逆に言うと、ここの設計を間違わなければ、無駄に同じようなオブジェクトを | |
// つくらないでいい | |
function Person (name) { | |
this.name = name || 'unknown'; | |
} | |
(function (P) { | |
P.say = function (prop) { | |
return (this[prop]) | |
? this[prop] | |
: ['faild: not found prop "', prop, + '".' ].join(''); | |
}; | |
P.ORG = 'NAKATA'; | |
P.setOrg = function (org) { | |
if (org) P.ORG = org; | |
return P.ORG; | |
}; | |
})(Person.prototype); | |
var keiko = new Person ('Keiko'); | |
var yukio = new Person ('Yukio'); | |
test(keiko.say('name'),'Keiko'); | |
test(keiko.say('ORG'), 'NAKATA'); | |
test(yukio.say('name'), 'Yukio'); | |
test(yukio.say('ORG'), 'NAKATA'); | |
keiko.setOrg('tanaka'); // ココ | |
test(keiko.say('ORG'), 'tanaka'); | |
test(yukio.say('ORG'), 'tanaka'); // NAKATA ではない | |
/* Functions */ | |
function test (funcs_result, forecast) { | |
var is_success = (funcs_result === forecast) ? 'success' : '!! faild'; | |
console.log([is_success, funcs_result, forecast].join('\t')); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment