社å
ã®ç²¾éã¨ã³ã¸ãã¢ãä¸å¿ã«å®æçã«åå¼·ä¼ããããã¨ã«ãªã£ãããã§ã JavaScript ã®è¬ç¾©ã¯åããããã¨ã«ãªã£ãã
è³æã社å
ã ãã§ã¨ã©ãã¦ããã®ã¯ãã£ãããªãã®ã§ãããã«å
¬éãã¦ããã¾ãã社å
ã®äººã社å¤ã®äººãèªãã§ãã ããã
è¬ç¾©ã®å 容ã¯åºæ¬çã«ã½ã¼ã¹å«ãã½ã¼ã¹ã¬ãã¥ã¼å½¢å¼ã
â»ã¿ã¼ã²ãã㯠JavaScript ã¯æ¸ãããã¨ãªãããªãã¸ã§ã¯ãæåè¨èªããã°ã©ãã
Section 00 Prototype.js ã®åã« JavaScript ã®ãªãã¸ã§ã¯ãã®æ¦è¦ã»ã»ã»
ãªãã¸ã§ã¯ããä½ã£ã¦ã¿ãã
var object = {};
ãªãã¸ã§ã¯ãã«ã¡ã½ããã¨ãããããã£ã追å ãã¦ã¿ãã
var object = { field: 'ITæ¦å£«', method: function() { alert('hello ' + this.field); } }; object.method();
å¾ã§é¢æ°ãä»ãããã§ãããé¢æ°å ã® this ã¯å®è¡æã«æ±ºã¾ãã
var object = { field: 'ITæ¦å£«' }; function func() { alert('hello ' + this.field); } object.method = func; object.method();
å¼ã³åºãã¨ãã«ç´æ¥ this ãä¸ãããã¨ãåºæ¥ãã
function func() { alert('hello ' + this.field); } var object = { field: 'ITæ¦å£«' }; func.apply(object); // func.call(object);
åãå½¢å¼ã®ãªãã¸ã§ã¯ãã大éçç£ãããå ´åãããã以ä¸ã®ããã«ããã
é¢æ°ã® prototype ããããã£ã«å¤§éçç£ããããªãã¸ã§ã¯ããå
¥ãããnew ãä»ãã¦é¢æ°ãå¼ã³åºãã¦ãªãã¸ã§ã¯ãã®ã³ãã¼ã大éçç£ããã
function factory() {} factory.prototype = { field: 'ITæ¦å£«', method: function() { alert('hello ' + this.field); } }; var object00 = new factory(); var object01 = new factory(); var object02 = new factory();
new ãä»ãã¦å¼ã³åºãã¨ã³ãã¼ããããªãã¸ã§ã¯ãã this ã¨ãªã£ã¦é¢æ°ãå¼ã°ãããããªãã¡ãåæåãè¡ããã¨ãåºæ¥ãã
function factory() { this.field = 'ITæ¦å£«'; } factory.prototype = { method: function() { alert('hello ' + this.field); } }; var object00 = new factory(); var object01 = new factory(); var object02 = new factory();
ã¡ãªã¿ã«ä¸ã®äºã¤ã®é¢æ°å®£è¨ã¯ã»ã¨ãã©åãæå³ã(ã¡ãã£ã¨éããã©)
function factory() {}
var factory = function() {};
ãããè¸ã¾ãã¦ããã£ãã Secsion 01 ã§ã¯ã©ã¹ãä½ãã¾ãã
Section 01 Class.create
Source 01 é常ã®ã¯ã©ã¹
var Human = function(name, hobby){ this.name = name || ''; this.hobby = hobby || ''; }; Human.prototype = { name: '', hobby: '', someThing1: 'hogehoge', someThing2 : 'hugahuga', someMethod1 : function() { alert(this.someThing1); }, someMethod2 : function() { alert(this.someThing2); }, toString: function() { var array = []; array.push('ååï¼', this.name || 'ãªã', ', 趣å³ï¼', this.hobby || 'ãªã'); return array.join(''); } }; var amano =new Human('天é','åå¼·'); amano.someMethod1(); amano.someMethod2(); alert(amano);
Source 02 ã³ã³ã¹ãã©ã¯ã¿ã¯ä¸ã«æ¸ããã
var Human = function(name, hobby){ this.intialize(name, hobby); }; Human.prototype = { name: '', hobby: '', someThing1: 'hogehoge', someThing2 : 'hugahuga', intialize: function(name, hobby){ this.name = name || ''; this.hobby = hobby || ''; }, someMethod1 : function() { alert(this.someThing1); }, someMethod2 : function() { alert(this.someThing2); }, toString: function() { var array = []; array.push('ååï¼', this.name || 'ãªã', ', 趣å³ï¼', this.hobby || 'ãªã'); return array.join(''); } }; var amano =new Human('天é','åå¼·'); amano.someMethod1(); amano.someMethod2(); alert(amano);
Source 03 Class.create ã使ãã¨ã»ã»ã»
var Human = Class.create(); Human.prototype = { name: '', hobby: '', someThing1: 'hogehoge', someThing2 : 'hugahuga', initialize : function(name, hobby) { this.name = name || ''; this.hobby = hobby || ''; }, someMethod1 : function() { alert(this.someThing1); }, someMethod2 : function() { alert(this.someThing2); }, toString: function() { var array = []; array.push('ååï¼', this.name || 'ãªã', ', 趣å³ï¼', this.hobby || 'ãªã'); return array.join(''); } }; var amano =new Human('天é','åå¼·'); amano.someMethod1(); amano.someMethod2(); alert(amano);
Section 02 Object.extend
Source 01 é常ã®ç¶æ¿
var Human = Class.create(); Human.prototype = { name: '', hobby: '', someThing1: 'hogehoge', someThing2 : 'hugahuga', initialize : function(name, hobby) { this.name = name || ''; this.hobby = hobby || ''; }, someMethod1 : function() { alert(this.someThing1); }, someMethod2 : function() { alert(this.someThing2); }, toString: function() { var array = []; array.push('ååï¼', this.name || 'ãªã', ', 趣å³ï¼', this.hobby || 'ãªã'); return array.join(''); } }; var Programmer = Class.create(); Programmer.prototype = new Human(); Programmer.prototype.programmingLanguage = ''; Programmer.prototype.initialize = function(name, hobby, programmingLanguage) { Human.prototype.initialize.apply(this, [name, hobby]); this.programmingLanguage = programmingLanguage; }; Programmer.prototype.toString = function() { var array = []; array.push(Human.prototype.toString.apply(this), ', ããã°ã©ãã³ã°è¨èªï¼', this.programmingLanguage || 'ãªã'); return array.join(''); } var amano =new Programmer('天é','åå¼·', 'JavaScript'); amano.someMethod1(); amano.someMethod2(); alert(amano); // Human ã®å®ç¾©ãå¤æ´ Human.prototype.toString = function() { var array = []; array.push('ååï¼', this.name || 'ãªã'); // 趣å³ã®è¡¨ç¤ºãããã return array.join(''); return ''; }; alert(amano);
Source 02 Object.extend ã使ãã¨ã»ã»ã»
var Human = Class.create(); Human.prototype = { name: '', hobby: '', someThing1: 'hogehoge', someThing2 : 'hugahuga', initialize : function(name, hobby) { this.name = name || ''; this.hobby = hobby || ''; }, someMethod1 : function() { alert(this.someThing1); }, someMethod2 : function() { alert(this.someThing2); }, toString: function() { var array = []; array.push('ååï¼', this.name || 'ãªã', ', 趣å³ï¼', this.hobby || 'ãªã'); return array.join(''); } }; var Programmer = Class.create(); Programmer.prototype = Object.extend(new Human(), { programmingLanguage: '', initialize: function(name, hobby, programmingLanguage) { Human.prototype.initialize.apply(this, [name, hobby]); this.programmingLanguage = programmingLanguage; }, toString: function() { var array = []; array.push(Human.prototype.toString.apply(this), ', ããã°ã©ãã³ã°è¨èªï¼', this.programmingLanguage || 'ãªã'); return array.join(''); } }); var amano =new Programmer('天é','åå¼·', 'JavaScript'); amano.someMethod1(); amano.someMethod2(); alert(amano); // Human ã®å®ç¾©ãå¤æ´ Human.prototype.toString = function() { var array = []; array.push('ååï¼', this.hobby || 'ãªã'); // 趣å³ã®è¡¨ç¤ºãããã return array.join(''); return ''; }; alert(amano);
Source 03 Object.extend ã¯ç¶æ¿ã®ããã«ãã訳ã§ã¯ãªã
var Human = Class.create(); Human.prototype = { name: '', hobby: '', someThing1: 'hogehoge', someThing2 : 'hugahuga', initialize : function(name, hobby, options) { options = Object.extend({ sex: 'ãªã', age: 'ãªã', location: 'ãªã' }, options || {}); Object.extend(this, options); this.name = name || ''; this.hobby = hobby || ''; }, someMethod1 : function() { alert(this.someThing1); }, someMethod2 : function() { alert(this.someThing2); }, toString: function() { var array = []; array.push('ååï¼', this.name || 'ãªã', ', 趣å³ï¼', this.hobby || 'ãªã'); array.push(', æ§å¥ï¼', this.sex, ', å¹´é½¢ï¼', this.age, ', å°åï¼', this.location); return array.join(''); } }; var Programmer = Class.create(); Programmer.prototype = Object.extend(new Human(), { programmingLanguage: '', initialize: function(name, hobby, programmingLanguage, options) { Human.prototype.initialize.apply(this, [name, hobby, options]); this.programmingLanguage = programmingLanguage; }, toString: function() { var array = []; array.push(Human.prototype.toString.apply(this), ', ããã°ã©ãã³ã°è¨èªï¼', this.programmingLanguage || 'ãªã'); return array.join(''); } }); // 天éèªç var amano =new Programmer('天é','åå¼·', 'JavaScript', { sex: 'ãã¨ã' }); amano.someMethod1(); amano.someMethod2(); alert(amano);
Section 03 Array.prototype.each
Source 01 é åã®è¦ç´ èµ°æ»
// Section 02 ããã³ Section 03 ã§ä½ã£ã Human 㨠Programmer ã使ç¨ããã var humans = [ new Programmer('ããã®','ã¬ã´', 'Java'), new Programmer('ZIGOROã¥','Perl', 'Perl', { location: 'ãã³ã' }), new Human('天é','åå¼·') ]; for(var i = 0, length = humans.length; i < length; i ++) { alert(humans[i]); }
Source 02 Array.prototype.each ã使ã£ãé åèµ°æ»
// Section 02 ããã³ Section 03 ã§ä½ã£ã Human 㨠Programmer ã使ç¨ããã [ new Programmer('ããã®','ã¬ã´', 'Java'), new Programmer('ZIGOROã¥','Perl', 'Perl', { location: 'ãã³ã' }), new Human('天é','åå¼·') ].each(alert);
Section 04 Function.prototype.bind
Source 01 é常ã®ã¤ã³ã¹ã¿ã³ã¹ã¡ã½ããã使ã£ãã³ã¼ã«ããã¯
// Section 02 ããã³ Section 03 ã§ä½ã£ã Human 㨠Programmer ã使ç¨ããã var Room = Class.create(); Room.prototype = { initialize: function() { this.members = []; }, addHuman: function(human) { this.members.push(human); }, toString: function() { return this.members.length + '人'; } }; var room = new Room(); [ new Programmer('ããã®','ã¬ã´', 'Java'), new Programmer('ZIGOROã¥','Perl', 'Perl', { location: 'ãã³ã' }), new Human('天é','åå¼·') ].each(function(human) { room.addHuman(human) }); alert(room);
Source 02 Function.prototype.bind ã使ã£ãã¤ã³ã¹ã¿ã³ã¹ã¡ã½ããã使ã£ãã³ã¼ã«ããã¯
// Section 02 ããã³ Section 03 ã§ä½ã£ã Human 㨠Programmer ã使ç¨ããã var Room = Class.create(); Room.prototype = { initialize: function() { this.members = []; }, addHuman: function(human) { this.members.push(human); }, toString: function() { return this.members.length + '人'; } }; var room = new Room(); [ new Programmer('ããã®','ã¬ã´', 'Java'), new Programmer('ZIGOROã¥','Perl', 'Perl', { location: 'ãã³ã' }), new Human('天é','åå¼·') ].each(room.addHuman.bind(room)); alert(room);