我一直在嘗試理解JavaScript中的繼承,到目前爲止,我已經閱讀了很多有關這方面的網站(包括javascript.info和Crockford的Javascript良好部分) - 但我可以似乎並不像Array繼承那麼簡單。JavaScript繼承和數組對象(需要理解的解釋)
也許如果我用一個例子來演示,有人可以糾正我,教我什麼我錯了。
function ExtendedArray() {
Array.call(this, arguments);
this.test = function() {
return 'something';
}
}
//I think this is the most standard way to extend a class?
ExtendedArray.prototype = [];
ExtendedArray.prototype.constructor = ExtendedArray;
$scope = {};
$scope.arr = new Array(1, 2, 3);
$scope.concatArr = [].concat($scope.arr);
$scope.x = new ExtendedArray(1, 2, 3); //empty | instanceof Array = true
$scope.concatX = [].concat($scope.x); //empty
$scope.y = new ExtendedArray(); //instanceof Array = true
$scope.y.push(1, 2, 3); //works - elements are added!
$scope.concatY = [].concat($scope.y); //concats it like a object
這裏是一個JS-搗鼓相同:
http://jsfiddle.net/superasn/pq2j139c/
一些問題:
- 我該如何解決這個代碼,以便
ExtendedArray
表現爲陣列?你可以看到$scope.x
是空的。爲什麼構造函數不工作? push
函數起作用!?但然後concat
失敗?如何使concat
工作?- 我看到有一些庫可以擴展類,是JS繼承的更好方法嗎?
您的建議表示讚賞!
'[] .concat($ scope.x)'是不是** **空,它包含的一個實例'ExtendedArray'對象 – CodingIntrigue
我剛剛從[這個問題]找到[這篇文章](http://perfectionkills.com/how-ecmascript-5-still-does-not-allow-to-subclass-an-array/)( http://stackoverflow.com/questions/3261587/subclassing-javascript-arrays-typeerror-array-prototype-tostring-is-not-generi) - 似乎相關 – CodingIntrigue
謝謝..是的,我也發現昨天的perfectionkills文章,它只是增加了我的困惑。當事情開始進入奇怪的領域時,我停止閱讀,他使用Iframes來實現像數組繼承一樣簡單的事情。我今天再給它一次。 – supersan