Bài 4: TẠO ĐỐI TƯỢNG TRONG javascript
1. Định nghĩa thuộc tính của đối tượng:
function student(name,age, grade) {
this.name = name;
this.age = age;
this.grade = grade;
}
Để tạo một Object ta sử dụng phát biểu new.Ví dụ để tạo đối tượng student1
student1 = new student(“Bob”,10,75);
3 thuộc tính của đối tượng student1 là :
student1.name
student1.age
student1.grade
Ví dụ để tạo đối tượng student2
student2 = new student(“Jane”,9,82);
Để thêm thuộc tính cho student1 bạn có thể làm như sau:
student1.mother = “Susan”; hoặc bạn có thể định nghĩa lại hàm student
MTWRFSS 4
function student(name, age, grade, mother) {
this.name = name;
this.age = age;
this.grade = grade;
this.mother = mother;
}
javascript MEDIASPACE CLUB (HTD) PAGE: 13
Đối tượng là thuộc tính của đối tượng khác
Ví dụ:
function grade (math, english, science) {
this.math = math;
this.english = english;
this.science = science;
}
bobGrade = new grade(75,80,77);
janeGrade = new grade(82,88,75);
student1 = new student(“Bob”,10,bobGrade);
student2 = new student(“Jane”,9,janeGrade);
student1.grade.math:dùng để lấy điểm Toán của student1
student2.grade.science: dùng lấy điểm môn Khoa học của student2
2. Thêm phương pháp cho đối tượng:
function displayProfile() {
document.write(“Name: “ + this.name + “
”);
document.write(“Age: “ + this.age + “
”);
document.write(“Mother’s Name: “ + this.mother + “
”);
document.write(“Math Grade: “ + this.grade.math + “
”);
document.write(“English Grade: “ + this.grade.english + “
”);
document.write(“Science Grade: “ + this.grade.science + “
”);
}
function student(name,age, grade) {
this.name = name;
this.age = age;
this.grade = grade;
this.mother = mother;
this.displayProfile = displayProfile;
}
student1.displayProfile();
Ví du:
4
Example 4.3<script LANGUAGE=”javascript”>
<!-- HIDE FROM OTHER BROWSERS
//DEFINE METHOD
function displayInfo() {
document.write(“
Employee Profile:
“ + this.name + “
”);
document.writeln(“Employee Number: “
+ this.number);
javascript MEDIASPACE CLUB (HTD) PAGE: 14
document.writeln(“Social Security
Number: “ + this.socsec);
document.writeln(“Annual Salary: “ +
this.salary);
document.write(“
”);
}
//DEFINE OBJECT
function employee() {
this.name=prompt(“Enter Employee’s
Name”,”Name”);
this.number=prompt(“Enter Employee
Number for “ + this.name,”000-000");
this.socsec=prompt(“Enter Social
Security Number for “ +
this.name,”000-00-0000");
this.salary=prompt(“Enter Annual
Salary for “ + this.name,”$00,000");
this.displayInfo=displayInfo;
}
newEmployee=new employee();
// STOP HIDING FROM OTHER BROWSERS --
>
<script LANGUAGE=”javascript”>
<!-- HIDE FROM OTHER BROWSERS
newEmployee.displayInfo();
// STOP HIDING FROM OTHER BROWSERS --
>
Vi du:
<script LANGUAGE="javascript">
<!-- Begin
var day="";
var month="";
var ampm="";
var ampmhour="";
var myweekday="";
var year="";
mydate = new Date();
myday = mydate.getDay();
mymonth = mydate.getMonth();
myweekday= mydate.getDate();
weekday= myweekday;
myyear= mydate.getYear();
year = myyear;
myhours = mydate.getHours();
ampmhour = (myhours > 12) ? myhours -
12 : myhours;
ampm = (myhours >= 12) ? 'Buổ i Chiề u '
: ' Buổ i Sá ng ';
mytime = mydate.getMinutes();
myminutes = ((mytime < 10) ? ':0' :
':') + mytime;
if(myday == 0)
day = " Chủ Nhậ t , ";
else if(myday == 1)
day = " Thứ hai, ";
else if(myday == 2)
day = " Thứ ba, ";
else if(myday == 3)
javascript MEDIASPACE CLUB (HTD) PAGE: 15
day = " Thứ tư, ";
else if(myday == 4)
day = " Thứ nă m, ";
else if(myday == 5)
day = " Thứ sá u , ";
else if(myday == 6)
day = " Thứ bả y , ";
if(mymonth == 0) {
month = "thá ng mộ t ";}
else if(mymonth ==1)
month = "thá ng hai ";
else if(mymonth ==2)
month = "thá ng ba ";
else if(mymonth ==3)
month = "thá ng tư ";
else if(mymonth ==4)
month = "thá ng nă m, ";
else if(mymonth ==5)
month = "thá ng sá u ";
else if(mymonth ==6)
month = "thá ng bả y ";
else if(mymonth ==7)
month = "thá ng tá m ";
else if(mymonth ==8)
month = "thá ng chín ";
else if(mymonth ==9)
month = "thá ng mườ i ";
else if(mymonth ==10)
month = "thá ng mườ i mộ t ";
else if(mymonth ==11)
month = "thá ng mườ i hai ";
// End -->
Trong phần body bạn có thể xuất ra dạng như sau:
<script>
document.write("
" + ampmhour + "" + myminutes + ampm)
document.write(" - " + day + " ngà y " + myweekday +" ");
document.write( month + " , nă m " + year + "");