林中誠 動態網頁設計 20121129 作業 林中誠 動態網頁設計 20121129 作業
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>Ch8_2_2.htm</title>
</head>
<body>
<h2>使用物件字面值建立物件方法</h2>
<hr/>
<script>
// 建立自訂物件
var objCard = {
  name : "陳小安",
  age  : 20,
  phone: "02-22222222",
  email: "hueyan@ms2.hinet.net",
  printCard: function() {
     document.write("姓名 : " + this.name + "<br/>");
     document.write("年齡 : " + this.age + "<br/>");
     document.write("電話 : " + this.phone + "<br/>");
     document.write("電郵 : " + this.email + "<br/>");
  }
};
// 呼叫物件方法
objCard.printCard();
</script>
</body>
</html>
林中誠 動態網頁設計 20121129 作業 林中誠 動態網頁設計 20121129 作業

 <!DOCTYPE html>

<html>
<head>
<meta charset="utf-8"/>
<title>Ch8_3_2.htm</title>
<script> 
// JavaScript函數
function printCard() {
   document.write("姓名 : " + this.name + "<br/>");
   document.write("年齡 : " + this.age + "<br/>");
   document.write("電話 : " + this.phone + "<br/>");
   document.write("電郵 : " + this.email + "<br/><hr/>");
}
</script>
</head>
<body>
<h2>在自訂物件新增方法</h2>
<hr/>
<script>

var objCard = new Object();

objCard.name = "江小魚";
objCard.age = 20;
objCard.phone = "03-33333333";
objCard.email = "
smallfish@yahoo.com.tw";

objCard.print = printCard;

objCard.displayName = function() {
   document.write("姓名 : " + this.name + "<br/>");
   document.write("年齡 : " + this.age + "<br/>");
   document.write("電話 : " + this.phone + "<br/>");
   document.write("電郵 : " + this.email + "<br/><hr/>");
}

objCard.print();
objCard.displayName();
</script>
</body>
</html>