����� 4
�� 6: ����� ������� ����.
��������� ������ �� ����� ���������� ���� ��� ������. ��� ����� �� ����� ������ �� ����� ��� ������ �� ������. �� ����� ������ ������ ���� ���� ���� ���� ����� �������� ������: ����� ���� ���� ��������� �-JavaScript.
������ ������, ���� ��� �� ������� ������� �- objectification �� ����� ����� �������� �����������, ������� ��� ���� �������� �� ���� ������ �� ������ �������. ��� ���� �� ��, �����, ������, ��� �����, �����, ��� ����. ������ ������ ���� �����, ���� ������, ������, �� ����� ����� ����. ������� ����� �� �� ����������� ���, �������� ����� ��� ��� ����� ����� ��� �������. ��� ���� �� �� ���� ������ �� ������ � ���, ��, ����� � ��� ������ �� ������� ���� ����� ����� ��� ������. �'� ��� ���� �� ��� ������ ���, ��'��� ��� ����� ����� �� ��� ������ ������. ����� �� ������, �� �� �� ��� �� ���� �������. ������ �� ������� �� ��� ������� �� ����� ��� �������.
�� ��� ����� ������� ����, ���� ������ �� ����� (Template). ������ ����� �����, ������ ����� ����� �������� (Object Constructor). ���� ����� �� ������, ��� ���� ����� ������ ����� �� ��������, ��� ��� ����:
var fred = new Employee("Fred Flintstone", 33, "Surface Miner", 20000);
var barney = new Employee("Barney Rubble", 33, "Slacker", 40000);
var boss = new Employee("Mr. Slate",50, "CEO", 1000000);
���� ����� �� ������ ��� �� �������� �������, ��� ���� ���� ������ ������, ����:
barney.promotion("Chief Slacker","10");
fred.fired();
boss.vacation(365);
�� ��� ���� ���� �� ���� ���� ������� ���� �� ����� �� 10 ������, ���� �� ���, ������ �� ����"� ���� ��� ����.
����� ����� ����� �� ������� ����� �������� ��� ������� ������� ����. ���� �� �� �������. ���� ��� ������ ������� � ����:
function Employee(name, age, title, salary)
{
this.name = name;
this.age = age;
this.title = title;
this.salary = salary;
}
��� �� �� ������ ��� �� �������. ���� �������� ���� ����� ����� �- this.property_name. ����� ��� ������ �� age ����� 0, ��� ������ �� this.age ����� 0. ���� ��� ���� �� ���� this.name, this.title ���'.
���� ������ ������� �� �����, ���� ��� ������� ���� ...
var barney = new Employee("Barney Rubble", 33, "Slacker", 40000);
this.name = name;
��� ������ �� ��� �� ���� �� �� ���� �� ������ �� ������� ���� �����.
����� ��� ������ "����" ("this") ������, ����� �� ��� ����� ���� ����� ��� ��� ���. �� ��� �������� ��� ��������� ��� �����, ���� ����� ���� �� ���� ���� ��� ����� ����. ���� ����� ������ �� "this": ���� �������� ������. ���� ������� ����� ������ �� ������.
������ �� ��� �������� ��� ������� ����������. ���� ��, ���� �� ��������, ��� ���� ���� �������� (���� ������� ������ �� ��������). �� �� ����� ()promotion ������:
function promotion(new_title,percent_raise)
{
var salary_increase = this.salary * percent_raise;
this.salary = this.salary + salary_increase;
this.title = new_title;
}
������� �� ����� ��� ������� ����� �� ����� ������ ���� ��, ��� �� ������ ����� �����. JavaScript ����� �� ���� ���� ����� ����� ������ �-"this". �� ��� ����:
barney.promotion("Chief Slacker",10);
�� "this" ��� barney. ���� ���� ��� ���� ������ ����� ��� ��� ������ ���, �� ���� ���� ����� ����� ������� �� ���������, ���� ������� ���� ������.
���� ������ ������ ������� ��� ����� �� ������ ��������. ��� ������� ���� ���, ����� ��� ���� ������. �� ��� ����� �� ����� ������ �������� �����, ���� ����� �� ������� ()promotion, ��� ������ ���� �����:
this.promotion=promotion;
��� ����� ����� ���� ����� ����� ������:
function Employee(name, age, title, salary)
{
this.name = name;
this.age = age;
this.title = title;
this.salary = salary;
this.promotion = promotion;
}
function promotion(new_title,percent_raise)
{
var salary_increase = this.salary * percent_raise;
this.salary = this.salary + salary_increase;
this.title = new_title;
}
������ ����������, ���, ����� �� ����� �����, ����� ����� ������ "����". ���, �� ���� ����� ����� ������� �� �����, ����� ����� ������ ()tranfer.
���� ���? ����� ����� ������ ����� ����� ����� ����� ����, ��� ����� ���� ���� ���������.
���� ������ ���
��� ��� «--
|