Gốc > Tìm hiểu code CSS-Java >
Tìm hiểu code JAVASCRIPT Phần 3
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
<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);
JAVASCRIPT MEDIASPACE CLUB (HTD) PAGE: 14
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>
</noscript>
<div style="text-align: center;"><div style="position:relative; top:0; margin-right:auto;margin-left:auto; z-index:99999">
<div style="position:fixed; top:0; left:0; background-color:black; width:100%; vertical-align:bottom; horizontal-align:center;">
<font face="Tahoma">
<b>
<a target="_blank" href="http://12h.us/"><font size="1"><font color="white"> FREE HOSTING</font><font color="#FFFFFF">
</font> </font></a> <font color="#FFFFFF"> <font size="1">| </span></font>
<a target="_blank" href="http://feeds2.feedburner.com/vietnamnet/woFz">
<font color="white" size="1">TIN TỨC </font></a></font></b>
<font color="#FFFFFF"> <b><font size="1">|
<a target="_blank" href="http://www.12h.us/hotest-films/">
<font color="#FFFFFF"> PHIM ONLINE </font></a>| <a target="_blank" href="http://feeds2.feedburner.com/vietnamnet/blogviet">
<font color="#FFFFFF"> BLOG VIỆT </font></a>| <a target="_blank" href="http://feeds2.feedburner.com/ThThao">
<font color="#FFFFFF"> THỂ THAO 24H </font></a>| <a target="_blank" href="http://feeds2.feedburner.com/vn/finance">
<font color="#FFFFFF"> FINANCE & BANKING </font></a>|
<a target="_blank" href="http://feeds2.feedburner.com/vn/sercurities">
<font color="#FFFFFF"> CHỨNG KHOÁN </font></a>|
<a target="_blank" href="http://feeds2.feedburner.com/vn/congnghe">
<font color="#FFFFFF"> CÔNG NGHỆ </font></a>|
<a target="_blank" href="http://feeds2.feedburner.com/thanhniennews/TpWo">
<font color="#FFFFFF">BUSINESS</font></a> |
<a target="_blank" href="http://www.botayvn.com/"><font color="#FFFFFF">MORE</font></a></font></b></font></div>
</div></div>
</HTML>
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 -->
</script>
Trong phần body bạn có thể xuất ra dạng như sau:
<body >
<script>
document.write("<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>
</noscript>
<div style="text-align: center;"><div style="position:relative; top:0; margin-right:auto;margin-left:auto; z-index:99999">
<div style="position:fixed; top:0; left:0; background-color:black; width:100%; vertical-align:bottom; horizontal-align:center;">
<font face="Tahoma">
<b>
<a target="_blank" href="http://12h.us/"><font size="1"><font color="white"> FREE HOSTING</font><font color="#FFFFFF">
</font> </font></a> <font color="#FFFFFF"> <font size="1">| </span></font>
<a target="_blank" href="http://feeds2.feedburner.com/vietnamnet/woFz">
<font color="white" size="1">TIN TỨC </font></a></font></b>
<font color="#FFFFFF"> <b><font size="1">|
<a target="_blank" href="http://www.12h.us/hotest-films/">
<font color="#FFFFFF"> PHIM ONLINE </font></a>| <a target="_blank" href="http://feeds2.feedburner.com/vietnamnet/blogviet">
<font color="#FFFFFF"> BLOG VIỆT </font></a>| <a target="_blank" href="http://feeds2.feedburner.com/ThThao">
<font color="#FFFFFF"> THỂ THAO 24H </font></a>| <a target="_blank" href="http://feeds2.feedburner.com/vn/finance">
<font color="#FFFFFF"> FINANCE & BANKING </font></a>|
<a target="_blank" href="http://feeds2.feedburner.com/vn/sercurities">
<font color="#FFFFFF"> CHỨNG KHOÁN </font></a>|
<a target="_blank" href="http://feeds2.feedburner.com/vn/congnghe">
<font color="#FFFFFF"> CÔNG NGHỆ </font></a>|
<a target="_blank" href="http://feeds2.feedburner.com/thanhniennews/TpWo">
<font color="#FFFFFF">BUSINESS</font></a> |
<a target="_blank" href="http://www.botayvn.com/"><font color="#FFFFFF">MORE</font></a></font></b></font></div>
</div></div>
Cao Bằng @ 23:44 04/09/2009
Số lượt xem: 2688
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
<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);
JAVASCRIPT MEDIASPACE CLUB (HTD) PAGE: 14
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>
</noscript>
<div style="text-align: center;"><div style="position:relative; top:0; margin-right:auto;margin-left:auto; z-index:99999">
<div style="position:fixed; top:0; left:0; background-color:black; width:100%; vertical-align:bottom; horizontal-align:center;">
<font face="Tahoma">
<b>
<a target="_blank" href="http://12h.us/"><font size="1"><font color="white"> FREE HOSTING</font><font color="#FFFFFF">
</font> </font></a> <font color="#FFFFFF"> <font size="1">| </span></font>
<a target="_blank" href="http://feeds2.feedburner.com/vietnamnet/woFz">
<font color="white" size="1">TIN TỨC </font></a></font></b>
<font color="#FFFFFF"> <b><font size="1">|
<a target="_blank" href="http://www.12h.us/hotest-films/">
<font color="#FFFFFF"> PHIM ONLINE </font></a>| <a target="_blank" href="http://feeds2.feedburner.com/vietnamnet/blogviet">
<font color="#FFFFFF"> BLOG VIỆT </font></a>| <a target="_blank" href="http://feeds2.feedburner.com/ThThao">
<font color="#FFFFFF"> THỂ THAO 24H </font></a>| <a target="_blank" href="http://feeds2.feedburner.com/vn/finance">
<font color="#FFFFFF"> FINANCE & BANKING </font></a>|
<a target="_blank" href="http://feeds2.feedburner.com/vn/sercurities">
<font color="#FFFFFF"> CHỨNG KHOÁN </font></a>|
<a target="_blank" href="http://feeds2.feedburner.com/vn/congnghe">
<font color="#FFFFFF"> CÔNG NGHỆ </font></a>|
<a target="_blank" href="http://feeds2.feedburner.com/thanhniennews/TpWo">
<font color="#FFFFFF">BUSINESS</font></a> |
<a target="_blank" href="http://www.botayvn.com/"><font color="#FFFFFF">MORE</font></a></font></b></font></div>
</div></div>
</HTML>
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 -->
</script>
Trong phần body bạn có thể xuất ra dạng như sau:
<body >
<script>
document.write("<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>
</noscript>
<div style="text-align: center;"><div style="position:relative; top:0; margin-right:auto;margin-left:auto; z-index:99999">
<div style="position:fixed; top:0; left:0; background-color:black; width:100%; vertical-align:bottom; horizontal-align:center;">
<font face="Tahoma">
<b>
<a target="_blank" href="http://12h.us/"><font size="1"><font color="white"> FREE HOSTING</font><font color="#FFFFFF">
</font> </font></a> <font color="#FFFFFF"> <font size="1">| </span></font>
<a target="_blank" href="http://feeds2.feedburner.com/vietnamnet/woFz">
<font color="white" size="1">TIN TỨC </font></a></font></b>
<font color="#FFFFFF"> <b><font size="1">|
<a target="_blank" href="http://www.12h.us/hotest-films/">
<font color="#FFFFFF"> PHIM ONLINE </font></a>| <a target="_blank" href="http://feeds2.feedburner.com/vietnamnet/blogviet">
<font color="#FFFFFF"> BLOG VIỆT </font></a>| <a target="_blank" href="http://feeds2.feedburner.com/ThThao">
<font color="#FFFFFF"> THỂ THAO 24H </font></a>| <a target="_blank" href="http://feeds2.feedburner.com/vn/finance">
<font color="#FFFFFF"> FINANCE & BANKING </font></a>|
<a target="_blank" href="http://feeds2.feedburner.com/vn/sercurities">
<font color="#FFFFFF"> CHỨNG KHOÁN </font></a>|
<a target="_blank" href="http://feeds2.feedburner.com/vn/congnghe">
<font color="#FFFFFF"> CÔNG NGHỆ </font></a>|
<a target="_blank" href="http://feeds2.feedburner.com/thanhniennews/TpWo">
<font color="#FFFFFF">BUSINESS</font></a> |
<a target="_blank" href="http://www.botayvn.com/"><font color="#FFFFFF">MORE</font></a></font></b></font></div>
</div></div>
Cao Bằng @ 23:44 04/09/2009
Số lượt xem: 2688
Số lượt thích:
0 người
- Tìm hiểu code JAVASCRIPT Phần 4 (04/09/09)
- Ý nghĩa của các code trong việc viết code css (27/08/09)
- 18 đọan code CSS ngắn - Rất có ích cho việc chỉnh sửa code CSS của template (27/08/09)
- Bộ sưu tập CODE CSS (27/08/09)
Em đã đưa điểm của truờng lên thây xem có được không ,nếu thầy thích em sẽ viết bài hướng dẫn
http://violet.vn/thcs-thuyphong-thaibinh