[問題][js] closure 和 prototype的差別~

看板Ajax作者 (MAI舞)時間16年前 (2009/06/02 22:59), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串1/2 (看更多)
(問題一) 在這個網頁==> http://caterpillar.onlyfun.net/Gossip/AjaxGossip/ConstructorPrototype.html 裡提到的 getSize 的建立方式分別為closure 和 prototype的方式 請問差別在哪呢? 不知怎麼理解^^" 是記憶體的佔用方式有差嗎?還是? (問題二) 在犀牛書上看到的 function Rectangle(w,h) { this.width = w; this.height = h; this.area = function() { return this.width*this.height;} } 和 function Rectangle(w,h) { this.width = w; this.height = h; } Rectangle.protoype.area = function() { return this.width* this.height; } 這兩種的area差別要怎麼看呢? 煩請各位前賢講解一下了<(_ _)> -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.113.95.110 ※ 編輯: kevintwo 來自: 140.113.95.110 (06/03 11:14) ※ 編輯: kevintwo 來自: 140.113.95.110 (06/03 11:19)
文章代碼(AID): #1A9Jvjef (Ajax)
文章代碼(AID): #1A9Jvjef (Ajax)