我試圖使用擴展的陣列我應該把在constructor..Here ..what擴展的代碼是的CoffeeScript如何使用陣列
class List extends Array
constructor:()->
super arguments
this
list = new List("hello","world")
alert list[0]
似乎沒有工作..
我試圖使用擴展的陣列我應該把在constructor..Here ..what擴展的代碼是的CoffeeScript如何使用陣列
class List extends Array
constructor:()->
super arguments
this
list = new List("hello","world")
alert list[0]
似乎沒有工作..
我沒有使用咖啡腳本最近,所以這只是一種猜測,但也許是這樣的:
class List extends [].prototype
可能會奏效?
編輯:每「畝太短」的評論,它似乎問題不是咖啡劇本相關。您可能會發現這個相關的SO帖子很有用,因爲所選答案提供了兩種可能的解決方案來解決這個問題: Is this a reasonable way to 'subclass' a javascript array?
沒有簡單的方法從數組原型「繼承」。您應該使用組合物,即
class List
constructor:()->
this.array = new Array(arguments);
getArray :()->
this.array
list = new List("hello","world")
alert list.getArray()[0]
或者你會花你的時間來實現複雜的解決方案,當你試圖解析數組或訪問它的長度值,它就會失敗。
更多的問題:
http://perfectionkills.com/how-ecmascript-5-still-does-not-allow-to-subclass-an-array/
做一點谷歌搜索「的JavaScript子類陣」的,它並沒有真正的工作時間。 – 2012-03-13 16:50:21