prototype.js ã§ãã¶ã¤ã³ãã¿ã¼ã³ - Iterator
Ruby on Rails ã Catalyst ã®ãã©ã°ã¤ã³ãªããã§ã¯ prototype.js ã¨ãã JavaScript ã®ã©ã¤ãã©ãªã使ã£ã¦ãAjax ãµãã¼ããå®ç¾ãã¦ãã¾ããprototype.js ã¨ãã¬ã¼ã ã¯ã¼ã¯ãå¿ è¦ãª Ajax ã® JavaScript ã³ã¼ããåãåºãã¦ãããã®ã§ãRuby ããã°ã©ãã Perl ããã°ã©ã㯠JavaScript ã®å®è£ ãæèããªãã¦ã Ajax ãªã¤ã³ã¿ãã§ã¼ã¹ãä½ãããã¨ãã風ã«ãªã£ã¦ãã¾ãã
ãããªæã㧠prototype.js 㯠Ajax ãªé¨åã«æ³¨ç®ãéã¾ã£ã¦ãã¾ãããã»ãã«ã "Class-style OO" ãªãã¬ã¼ã ã¯ã¼ã¯ãå å ãã¦ã¾ãã
JavaScript ã¯ãããã¿ã¤ããã¼ã¹ã®ãªãã¸ã§ã¯ãæåè¨èªã§ãC++ ã Java ã®ãããªã¯ã©ã¹ãã¼ã¹ã®ãªãã¸ã§ã¯ãæåè¨èªã¨ã¯ã¡ãã£ã¨å®è£ ãç°ãªãã¾ãããããã¿ã¤ããã¼ã¹ã®ãªãã¸ã§ã¯ãæå...ã¯åãç解ãã¡ãã£ã¨ããã¾ããªã®ã§ä»ã®ãµã¤ãããå¼ç¨ãã¾ãã
ãªãã¸ã§ã¯ããã¹ãããï¼ã¯ã©ã¹ã®ã¤ã³ã¹ã¿ã³ã¹ãªãã¤ã³ã¹ã¿ã³ã¹å¤æ°ãã¡ã½ããã«ç¸å½ï¼ã®è¿½å ãã¯ã©ã¹ã«ä¾åããã«èªç±ã«ã§ãããã¨ãåæã¨ãããªãã¸ã§ã¯ãæåããããã¯ãããããªãã¸ã§ã¯ããç¨ããããã°ã©ãã³ã°ããããããµãã¼ãããæ©æ§ã
ã¤ã¾ããã¯ã©ã¹ãã¼ã¹ãª OO ã§ã¯ãã¯ã©ã¹ã«ã¤ã³ã¹ã¿ã³ã¹ã®æ§è³ª(ããããã£)ãæ¯ãèã(ã¡ã½ãã)ãå®ç¾©ããã¦ãã¦ããã®ã¯ã©ã¹ãéå½¢ã«ãªãã¸ã§ã¯ããçæãããã¨ããæãã§ããããããã¿ã¤ããã¼ã¹ OO ã§ã¯ã¾ãå ã«å®ä½ã¨ãã¦ã®ãããã¿ã¤ãããã£ã¦ãããã«æ§è³ªãæ¯ãèããä»ãå ãã¦ãããã¨ã§ãªãã¸ã§ã¯ããå½¢æããã¦ãããã¨ããæãã§ãã
ãããªãã㧠JavaScript ã® OO ã¯é常ã
/* ã³ã³ã¹ãã©ã¯ã¿ */ function Dog (name) { this.name = name; } /* Dog ã®ã¡ã½ãã */ Dog.prototype.bark = function () { alert(this.name + ': ãããã'); } dog = new Dog('ããªãã'); dog.bark();
ã¿ãããªæãã§å®è£ ãã¾ããJavaScript ã§ã¯é¢æ°ããªãã¸ã§ã¯ããªã®ã§ãã³ã³ã¹ãã©ã¯ã¿ã¯ Dog ã¨ããã©ãã«ã®ä»ããé¢æ°ã¨ãã¦å®è£ ããããã®ã¯ã©ã¹ã®ãããã¿ã¤ãã« bark() ã¡ã½ãããå®è£ ããã¨ãã£ãå ·åã
ã§ãprototype.js ã«å«ã¾ãã¦ãã Class-Style OO ãªæ©è½ã使ãã¨ãJavaScript ã§ãããå°ãã¯ã©ã¹ãã¼ã¹é¢¨ãªã·ã³ã¿ãã¯ã¹ã§æ¸ããã¨ãã§ããã¨ããå¡©æ¢ ã§ããä¾ãã°å ã®ç¬ã®ä¾ã prototype.js 㪠OO ã«ããã¨ã
var Dog = Class.create(); Dog.prototype = { initialize : function (name) { this.name = name; }, bark : function () { alert(this.name + ': ãããã'); } } dog = new Dog('ããªãã'); dog.bark();
ã¨ãªãã¾ããprototype.js 㯠Class ã¨ããã¯ã©ã¹ãçæããã¯ã©ã¹(ç´ãããããª)æä¾ããã®ã§ãã¾ã
var Dog = Class.create();
ã¨ãã¦ã¯ã©ã¹ãä½ã£ã¦ããã¨ã¯ prototype ã«è²ã ä»ãããã¦ããã¨ãã¡ãªã¿ã«
Dog.prototype = { initialize : function (name) { this.name = name; }, bark : function () { alert(this.name + ': ãããã'); } }
ã®é¨åã®ã·ã³ã¿ãã¯ã¹ã¯ãã¾ãè¦æ £ããªãã®ã§ããã prototype.js ã®æ©è½ãªã®ããªã¨ä¸ç¬æã£ã¡ããã®ã§ããããã㯠JavaScript çµã¿è¾¼ã¿ã®ãªãã©ã«ã§ã
var obj = { x:1, y:2 };
ã¨æ¸ã㨠x ã 1ãy ã 2 ãªãªãã¸ã§ã¯ãã« obj ã¨ããååãã¤ãã¨ãããã®ã§ããã¤ã¾ãã
var obj = new Object; obj.x = 1; obj.y = 2;
ã¨åãã¨ãããã¨ãClass ã使ãã¨ã¤ã³ã¹ã¿ã³ã¹çææã« initialize ã¡ã½ãããå¼ã°ããããã«ãªã£ãããprototype.js ã«å«ã¾ãã Object.extend ã¨ä½µç¨ãããã¨ã§
var Dog = Class.create(); Dog.prototype = (new Animal).extend({ .... })
ã¨ããã·ã³ã¿ãã¯ã¹ã§ç¶æ¿ãå®ç¾ã§ãããããããã«ãªãã¾ãã
åç½®ããé·ããªãã¾ããããã® prototype.js ã使ã£ã¦ãã¶ã¤ã³ãã¿ã¼ã³ãªã³ã¼ããæ¸ãã¦ã¿ãã¨ããã®ãä»åã®è¶£æ¨ã§ããé¡æã¯ãã¡ããçµåããã®ãã¶ãã¿æ¬ãå¦ç¿ãã¦ãããã¤ãå®è£ ãã¦ã¿ãã®ã§æãã¿ã¦æ²è¼ãã¦ãããããªã¨ã(æ¬å½ã¯ãå®éã®ã¦ã§ãã¢ããªã±ã¼ã·ã§ã³ãé¡æã«ãã¶ã¤ã³ãã¿ã¼ã³ãé©ç¨ããã³ã¼ãã¨ãã®æ¹ãè¯ããããªãã§ããããããããçµé¨ãä¹ãããã®ã§ã©ãããå ´åã«ã¯ã©ã¤ã¢ã³ããµã¤ã JavaScript ã§ãã¶ã¤ã³ãã¿ã¼ã³ã使ããã¨ãããããã¾ã ã¾ã£ããæ´ãã¦ãªãã£ããã)
ã¾ã㯠Iterator ãã¿ã¼ã³ã§ãã
ãã¶ã¤ã³ãã¿ã¼ã³ã®ã³ã¼ãã¯ã¾ããã®ã¯ã©ã¹ç¾¤ãå©ç¨ããã³ã¼ãããè¦ã¦ããã®ãããããããã§ããIterator ãã¿ã¼ã³ã§ã¯
var Main = Class.create(); Main.prototype = { initialize : function () {}, main : function () { var shelf = new BookShelf(); shelf.appendBook(new Book('å®è·µãã¤ããã©ã¼ãã³ã¹ MySQL')); shelf.appendBook(new Book('Perlã¯ãã¯ããã¯')); shelf.appendBook(new Book('Blog Hacks')); var it = shelf.iterator(); while (it.hasNext()) { var book = it.next(); document.writeln(book.getName() + '<br>'); } } }
ã¨ããã³ã¼ãããã£ã¦ã
var client = new Main; client.main();
ã¨å®è¡ããã¨ã
å®è·µãã¤ããã©ã¼ãã³ã¹ MySQL Perlã¯ãã¯ãã㯠Blog Hacks
ã¨è¡¨ç¤ºããããããªããã°ã©ã ã§ããèã¯
var it = shelf.iterator(); while (it.hasNext()) { var book = it.next(); document.writeln(book.getName() + '<br>'); }
ã§ãããshelf ã¨ããç©ã®éåä½ããã£ã¦ããããã¤ãã¬ã¼ã¿ãåãåºãã¦ããã¤ãåãã¨ãã®ç©ã辿ã£ã¦ããããç©ã®éåä½ã®å®è£ ãã©ããªãã¼ã¿æ§é ã§ãããåãã¤ã³ã¿ãã§ã¼ã¹ã§ä¸ã®ãã®ãã²ã¨ã¤ãã¤åãåºãã¦ãããã¨ãã§ãããã¨ã
ããã§ããã¶ãã¿æ¬ã®ã³ã¼ãã§ã¯ Java ã® interface ãã¾ãç¨æãã¦ãã¨ãªãã®ã§ããä¾ã«ãã£ã¦ JavaScript ã¯åçãªè¨èªãã¤åããªãã®ã§ãµãã¯ã©ã¹ã«å®è£ ãå¼·å¶ãããã¡ã«ããºã ããªããã¨ãããã¨ã§ãããã§ã¯ interface ã«ç¸å½ãããã®ã¯ç¨æããã« Concrete ã¯ã©ã¹ã ãã§å®è£ ãã¾ãã
ç»å ´ããã¯ã©ã¹ã¯ã
- Book
- BookShelf
- BookShelfIterator
ã® 3 ã¤ã«ãªãã¾ãã
ã¾ãã¯éåä½ã®ä¸ã®åã ã®ç©ã§ãã Book ã¯ã©ã¹ã
var Book = Class.create(); Book.prototype = { initialize : function(name) { this.name = name; }, getName : function() { return this.name; } }
æ¬ã®ã³ã¼ãã«ãªãã£ã¦ getName() ãç¨æãã¦ã¾ãããJavaScript çã«ã¯ book.name ã§ã¢ã¯ã»ã¹ãã¡ãã£ã¦ãããããã§ããã
次ãæ¬ã®å ¥ãç©ã§ããéåä½ã®ã¯ã©ã¹ã§ããã¨ããã® BookShelfããã¶ãã¿æ¬ã§ã¯ Concrete Aggregate ã«ç¸å½ãããã¤ã§ããã
var BookShelf = Class.create(); BookShelf.prototype = { initialize : function() { this.last = 0; this.books = new Array(); }, getBookAt : function(index) { return this.books[index]; }, appendBook : function(book) { this.books[this.last] = book; this.last++; }, getLength : function() { return this.last; }, iterator : function() { return new BookShelfIterator(this); } }
Java ã¿ããã«ã¯ã©ã¹ã®ä¸ã§ã¯ this ãçç¥ã§ããã¨ãããã ãã©ãããã¯ç¡çã£ã½ãã®ã§ãããªå¡©æ¢ ã«ãªãã¾ãããiterator ã¡ã½ããã§ã¤ãã¬ã¼ã¿ã¼ãè¿ãã¾ãã
var BookShelfIterator = Class.create(); BookShelfIterator.prototype = { initialize : function(bookshelf) { this.bookshelf = bookshelf; this.index = 0; }, hasNext : function () { return this.index < this.bookshelf.getLength(); }, next : function() { return this.bookshelf.getBookAt(this.index++); } }
ãã§ãã£ã¦ãã®ã¤ãã¬ã¼ã¿ãç¹ã«é£ããã¨ããããªãã
åã¯ãã®ã¯ã©ã¹ã®ã³ã¼ãããMain ãå«ã㦠iterator.js ã¨ããä¸ã«å ¨é¨æ¸ãã¦ãã£ã¦
<script type="text/javascript" src="../prototype.js"></script> <script type="text/javascript" src="iterator.js"></script> <script type="text/javascript"> var main = new Main(); main.main(); </script>
ã¨è¨è¿°ãã HTML ãç¨æãå®è¡ãã¦ã¾ãã
ã¡ã¨é·ããªãã¾ããããããªæãã§ä¸ã¤ä¸ã¤ã®ãã¿ã¼ã³ã®ã³ã¼ããè¦ãããã°ã©ãã³ã°ãã¦ãããããªã¨ã
å¢è£æ¹è¨çJavaè¨èªã§å¦ã¶ãã¶ã¤ã³ãã¿ã¼ã³å ¥é
- ä½è : çµå浩
- åºç社/ã¡ã¼ã«ã¼: ã½ãããã³ã¯ã¯ãªã¨ã¤ãã£ã
- çºå£²æ¥: 2004/06/19
- ã¡ãã£ã¢: 大åæ¬
- è³¼å ¥: 51人 ã¯ãªãã¯: 762å
- ãã®ååãå«ãããã° (397件) ãè¦ã