tag:blogger.com,1999:blog-16607461.post4960937766439351188..comments2024-03-29T10:21:47.284+08:00Comments on 布丁布丁吃什麼?: 談JavaScript使用prototype實作物件導向的探討布丁布丁吃布丁http://www.blogger.com/profile/13614721642960940190noreply@blogger.comBlogger6125tag:blogger.com,1999:blog-16607461.post-23642005350140683892013-01-13T16:29:14.421+08:002013-01-13T16:29:14.421+08:00To 5樓匿名,
你說的沒錯。
最近我在讀JavaScript設計模式,才發現以前的認知很多都是...To 5樓匿名,<br /><br />你說的沒錯。<br /><br />最近我在讀JavaScript設計模式,才發現以前的認知很多都是錯誤的。<br />特別是JavaScript沒有class這個概念很重要。<br />有志學習JavaScript的人請務必讀一下「JavaScript設計模式」這本書,定價480不貴也不厚,但是裡面滿滿的精華絕對讓你了之後會對之前寫的程式感到悔不當初!<br />http://www.taaze.tw/apredir.html?125290739/http://www.taaze.tw/sing.html?pid=11100603595布丁布丁吃布丁https://www.blogger.com/profile/18000418899714977849noreply@blogger.comtag:blogger.com,1999:blog-16607461.post-16708360920682758482013-01-12T20:41:12.743+08:002013-01-12T20:41:12.743+08:00這不是傳址問題, year 不直接在 constructor 裏面宣告而以「Ancestor.pro...這不是傳址問題, year 不直接在 constructor 裏面宣告而以「Ancestor.prototype.birthday= { year: null, month: null, day: null };」建立 year, 這個 year 算是外部共用的變數並不屬於 class 的一部份故不會繼承, 這是寫法錯誤問題。<br />Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-16607461.post-42551084182709372412011-04-01T22:28:43.055+08:002011-04-01T22:28:43.055+08:00嗯,我仔細地看了一下你程式的寫法跟結果
結果應該是跑出「189789」的值
這個意思是:
1. g...嗯,我仔細地看了一下你程式的寫法跟結果<br /><br />結果應該是跑出「189789」的值<br />這個意思是:<br />1. gf的靜態屬性foo的值並沒有被f覆蓋<br />2. gf的物件屬性S底下的foo1跟foo2,卻會被f覆蓋<br /><br />這還是不脫離我之前整理的JavaScript傳值與傳址心得<br />http://pulipuli.blogspot.com/2010/09/javascript.html<br /><br />簡單來說,類別的屬性如果是物件型態並且操作該物件底下的屬性,那麼就是用傳址來運作<br />如果類別的屬性是靜態型態,那麼就是用傳址來運作<br /><br />其實如果要Grandfather的S避免被覆蓋,set方法應該要改寫如下:<br /><br />Grandfather.prototype.Set = function (a,b,c)<br />{<br /> this.foo = a;<br /> this.S = new Object;<br /> this.S.foo1 = b;<br /> this.S.foo2 = c;<br />}; <br /><br />那麼gf.S跟f.S就會是完全不一樣的物件,而不會彼此受到影響<br /><br />-------------------<br /><br />GrandFather.call(this)只是重複再做一次宣告動作而已<br />但因為傳址影響的關係,所以重複做還是一樣會受到影響的喔<br /><br />-------------------<br /><br />不好意思隔了這麼久才回XDD<br />歡迎多多討論喔!布丁布丁吃布丁https://www.blogger.com/profile/13614721642960940190noreply@blogger.comtag:blogger.com,1999:blog-16607461.post-50347735636673864342011-01-28T14:38:28.654+08:002011-01-28T14:38:28.654+08:00不好意思, 上面的例子有問題...
在測試之後, 似乎是跟你的property 是struct or...不好意思, 上面的例子有問題...<br />在測試之後, 似乎是跟你的property 是struct or variable有關...<br />如果是struct 就會變成是傳reference, 如果是variable, 就會是value<br /><br />下面的code即可顯示區別<br /><br />function Grandfather() {<br /> this.Set(1,2,3);<br />}<br /><br />Grandfather.prototype.foo = 0;<br />Grandfather.prototype.S = {foo1:0,foo2:0};<br /><br />Grandfather.prototype.Set = function (a,b,c)<br />{<br /> this.foo = a;<br /> this.S.foo1 = b;<br /> this.S.foo2 = c;<br />}; <br /><br />function Father() {<br /> this.Set(7,8,9);<br />}<br /><br />Father.prototype = new Grandfather(); <br /><br />var gf = new Grandfather();<br />var f = new Father();<br /><br />document.write(gf.foo+'<br>');<br />document.write(gf.S.foo1+'<br>');<br />document.write(gf.S.foo2+'<br>');<br />document.write(f.foo+'<br>');<br />document.write(f.S.foo1+'<br>');<br />document.write(f.S.foo2+'<br>');<br /><br />但我試圖把你的 GrandFather.call(this)加進去, 但好像對struct而言, 值還是會錯...ylnoreply@blogger.comtag:blogger.com,1999:blog-16607461.post-10246963672938812592011-01-28T13:46:57.725+08:002011-01-28T13:46:57.725+08:00有點複雜,晚點再來研究看看@@"有點複雜,晚點再來研究看看@@"布丁布丁吃布丁https://www.blogger.com/profile/13614721642960940190noreply@blogger.comtag:blogger.com,1999:blog-16607461.post-17431673104932162722011-01-28T11:57:02.774+08:002011-01-28T11:57:02.774+08:00您好, 我對javascript不算熟, 不過你的結果可以用以下的code達成目的:
functi...您好, 我對javascript不算熟, 不過你的結果可以用以下的code達成目的:<br /><br />function Grandfather() {this.setBirthdayYear(1889);}<br /><br />Grandfather.prototype.setBirthdayYear = function (year)<br />{this.year = year;}; <br /><br />function Father() {this.setBirthdayYear(1915);}<br /><br />Father.prototype = new Grandfather(); <br /><br />function Son() {this.setBirthdayYear(1943);}<br /><br />Son.prototype = new Father(); <br /><br />function Daughter() {this.setBirthdayYear(1945);}<br /><br />Daughter.prototype = new Father();<br /><br />var grandfather = new Grandfather();<br />var father = new Father();<br />var son = new Son();<br />var daughter = new Daughter();<br /><br />如此即可得到最後的結果...<br />至於原因嘛... 我也不清楚 :pylnoreply@blogger.com