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

function student(name, age, grade, mother) {
this.name = name;
this.age = age;
this.grade = grade;
this.mother = mother;
}

Đố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 + "<BR>");
document.write("Age: " + this.age + "<BR>");
document.write("Mother's Name: " + this.mother + "<BR>");
document.write("Math Grade: " + this.grade.math + "<BR>");
document.write("English Grade: " + this.grade.english + "<BR>");
document.write("Science Grade: " + this.grade.science + "<BR>");
}

function student(name,age, grade) {
this.name = name;
this.age = age;
this.grade = grade;
this.mother = mother;
this.displayProfile = displayProfile;
}

student1.displayProfile();

Ví dụ 1:
<HTML>
<HEAD>
<TITLE>Example 4.3</TITLE>
<SCRIPT LANGUAGE="JavaScript">
<!-- HIDE FROM OTHER BROWSERS
//DEFINE METHOD
function displayInfo() {
document.write("<H1>Employee Profile: " + this.name + "</H1><HR><PRE>");
document.writeln("Employee Number: " + this.number);
document.writeln("Social Security Number: " + this.socsec);
document.writeln("Annual Salary: " + this.salary);
document.write("</PRE>");
}
//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>
</HEAD>
<BODY>
<SCRIPT LANGUAGE="JavaScript">
<!-- HIDE FROM OTHER BROWSERS
newEmployee.displayInfo();
// STOP HIDING FROM OTHER BROWSERS -->
</SCRIPT>
</BODY>
</HTML>

Ví dụ 2:
<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) ? ' Tố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)
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 -->
</script>

Trong phần body bạn có thể xuất ra dạng như sau:
<body>
<script>
document.write("<b><font color=#0000ff face='VNITimes,helvetica,arial'>" + ampmhour + "" + myminutes + ampm);
document.write(" - " + day + " ngày " + myweekday +" ");
document.write( month + " , năm " + year + "</font>");
</script>
</body>

DXOan

Đây chính là tôi với sự bùng nổ của những giấc mơ. Tôi thích khám phá và chia sẻ những điều mới mẻ. Hãy đồng hành cùng tôi nhé ! 😁😘😍 - Con đường tôi đi là con đường tôi chọn - 😎😂✌ facebook twitter pinterest youtube external-link

Đăng nhận xét

Vui lòng đưa ra những nhận xét tích cực, mang tính xây dựng.*

Mới hơn Cũ hơn

AdBlock Detected!

Phát hiện trình duyệt đang sử dụng AdBlock. Vui lòng tắt AdBlock để có thể xem nội dung cũng như ủng hộ tác giả. DXOan xin cảm ơn. Chúc bạn một ngày vui vẻ !