����� 4
�� 9: ���� ��������� ���� ����� (Hard to Reach Objects)
��� ������ �� Document Object Model Hierarchy ��� ����� ����� �������� ���� ������. ������ ��� �������� ������ �� ����� ���� ����� ������:
��� ���� ������ �������� ��:
function swapOne()
{
var the_image = prompt("change parrot or cheese","");
var the_image_object;
if (the_image == "parrot")
{
the_image_object = window.document.parrot;
} else {
the_image_object = window.document.cheese;
}
the_image_object.src = "ant.gif";
}
the_image_object = window.document.parrot;
����� �� ����� �� ������� ������ ���� ������. ����� ������ �� ����� ������ ����, ��� ����� �������. �� �� ���� �� �� �� 100 ������, ��� �� �����? ���� �� ����� if-then-else ����� ����. �� ��� ���� ���� ����� �� ����� ���:
function swapTwo()
{
var the_image = prompt("change parrot or cheese","");
window.document.the_image.src = "ant.gif";
}
����� ����, JavaScript ���� ����� ������ the_image ����� ���� ���� ���� ���� �- the_image ���� �"�����" - �� ������ ����� "�����" (cheese) (����� �- prompt), �� "����" (parrot) � �� ��� �� ��� ������ "����".
����� ����, eval ����� ����� �� ���� �� ������� ������ ���� ����:
function simpleSwap()
{
var the_image = prompt("change parrot or cheese","");
var the_image_name = "window.document." + the_image;
var the_image_object = eval(the_image_name);
the_image_object.src = "ant.gif";
}
�� ������ ����� "����" �� ��� ���� �- prompt, ����� ������ ����� ������ ������ ��� window.document.parrot. ��� �-eval ����� ������� ����: "�� �� �� �������� window.document.parrot" � ��� ��� ������� ������ ���� ����. ������� �� ������� ������ ������, ���� ����� �� �- source ��� �- ant.gif. �����, ��? ����� �� ���� ������, ������ ������� ��� ����.
�� eval �� ����� �� ������, ���� ��� ����� ���� ���������� ���� �����.
���� ������ ���
��� ��� «--
|