保存して new1, new2 の順でクリックして下さい。
<html>
<head>
</head>
<script type="text/javascript">
<!--
_str = "";
alert1 = function (str) {_str += str + "\n";};
alert2 = function () {alert(_str); _str="";};
new1q1 = "";
new1q2 = "";
function mynew(func)
{
var ret = {}, key;
if (typeof func == "function" &&
func.prototype && typeof func.prototype == "object")
{
for (key in func.prototype)
{
ret[key] = func.prototype[key];
}
ret.__construct = func;
ret.__construct();
delete ret.__construct;
ret.__test = "test";
}
return ret;
}
Base = {
food: "毒キノコ",
question: function()
{
return "あなたは" + this.food + "が好きですか?";
},
answer: function()
{
return "私は" + this.food + "が好きです。";
}
};
function q1() {}
function q2()
{
this.types = {
like: "好き",
hate: "嫌い",
unknown: "わからない"
};
this.type = "unknown";
this.answer = function()
{
return "私は" + this.food + "が" + this.types[this.type] + "です。";
};
}
function new1()
{
var obj1, obj2, key;
alert1("q1 を new");
q1.prototype = Base;
obj1 = new q1();
alert1(obj1.question());
obj1.food = "ベニテングダケ";
alert1(obj1.answer());
alert1("typeof: " + typeof obj1);
for (key in obj1)
{
alert1("key: " + key + ": " + typeof obj1[key]);
}
new1q1 = _str;
alert2();
alert1("q2 を new");
q2.prototype = obj1;
obj2 = new q2();
obj2.type = "hate";
alert1(obj2.question());
obj2.food = "ニガクリタケ";
alert1(obj2.answer());
alert1("typeof: " + typeof obj2);
for (key in obj2)
{
alert1("key: " + key + ": " + typeof obj2[key]);
}
new1q2 = _str;
alert2();
}
function new2()
{
var obj1, obj2, key;
alert1(new1q1);
alert1("q1 を mynew");
q1.prototype = Base;
obj1 = mynew(q1);
alert1(obj1.question());
obj1.food = "ベニテングダケ";
alert1(obj1.answer());
alert1("typeof: " + typeof obj1);
for (key in obj1)
{
alert1("key: " + key + ": " + typeof obj1[key]);
}
alert2();
alert1(new1q2);
alert1("q2 を mynew");
q2.prototype = obj1;
obj2 = mynew(q2);
obj2.type = "hate";
alert1(obj2.question());
obj2.food = "ニガクリタケ";
alert1(obj2.answer());
alert1("typeof: " + typeof obj2);
for (key in obj2)
{
alert1("key: " + key + ": " + typeof obj2[key]);
}
alert2();
}
//-->
</script>
<body>
<button onclick="javascript:new1();">new1</button>
<button onclick="javascript:new2();">new2</button>
</body>
</html>for (key in obj) { ... } はオブジェクト用です。
for (i = 0; i < arr.length; i++) { ... } は配列用です。
obj = {0:"one", 1:"two"};
arr = Array("one", "two");
for (key in obj) {alert(obj[key]);}
for (i = 0; i < arr.length; i++) {alert(arr[i]);}オブジェクトは 半角英字とアンダーバーとドル記号(2文字目から半角数字)だけの key の場合 obj.key と書けます。
__iterator__, __proto__ などがあるので __(アンダーバー2個)で始まる文字を使わないことをお勧めします。
また半角数字だけや、半角数字で始まる文字を使わないことをお勧めします。(obj.key と書けるため)
obj = {"0":"zero", "$":"one", "_":"two", "a":"a", "b001":"b001"};
//alert(obj.0);
alert(obj.$);
alert(obj._);
alert(obj.a);
alert(obj.b001);
alert(obj["0"]);
alert(obj["$"]);
alert(obj["_"]);
alert(obj["a"]);
alert(obj["b001"]);obj1.food = "ベニテングダケ"; が obj2.food に引き継がれていますが、この動作が予想外であるという原因で不具合が発生するかもしれません。その場合 q2.prototype = Base; です。
function q2() の中で Base で定義される内容を拡張していますが、この動作が作法に則っているかどうかは不明です。
function mynew(func) の内容が new をある程度エミュレートしていますが、これは参考なので new を使ってください。
newを使う場合のお勧めはベースとなるオブジェクトを利用してnewしたオブジェクトは別のnewに使わないことです。ベースとなるオブジェクトが100回newされた結果の値だとしても、あなたの管轄ではなく、ソースを見ないですむほどのマニュアルがあれば問題ないです。よって問題が発生する場合があります。
http://www.glamenv-septzen.net/view/170
0 件のコメント:
コメントを投稿