我真的很努力地找到非常簡單和實用的例子(例如在MDN),但它讓我發瘋。我不能簡單地弄清楚,我犯了錯誤。我想有一個Array對象的祖先。這裏是示例。如何在javascript中製作對象的子對象?
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Fiddling</title>
<script>
function Synthese() {
//Array.call(this);
//Synthese.prototype = Object.create(Array.prototype);
//Synthese.prototype.constructor = Synthese;
this.prototype = Object.create(Array);
//this.prototype.constructor = this;
this.Make = function() {
result = "";
for (i=0; i<this.length; i++){
result = result + this[i] + ".";
}
return result;
}
}
var A = new Array();
A.push("A"); //OK
var S = new Synthese();
S.push("A"); //fails
S.push("B");
alert(S.Make());
</script>
</head>
<body>
</body>
</html>
如何使Synthese成爲Array的子項? S.push( 「A」);從不執行
的[如何在JavaScript中的類繼承?]可能的複製(http://stackoverflow.com/questions/2107556/how-to-inherit-from設置原型-a-class-in-javascript) – AMACB
是否有錯誤? –
@ zer00ne:它可以是任何東西,這裏的字符串... – lyborko