📄 ex8-8.htm
字号:
<html>
<head><title>使用for...in循环访问对象的每一个属性 </title></head>
<script language = "javascript">
<!--
function phone(name, phoneno, site){ //姓名、电话号码、安装位置
// 定义属性
this.name = name;
this.phoneno = phoneno;
this.site = site;
this.rate = 0.3; //电话费率,0.3元/分钟
this.show=display; //函数在后面定义
this.getattr=getprops;
}
function display(anyphone){ //拼接字符串,并显示电话信息
with (anyphone){
var tmpstr="\n电话用户为:"+name+"\n电话号码:"+phoneno
+"\n安装位置:"+site+"\n费率:"+rate;
alert(tmpstr);
}
}
function getprops(theobj, name){ // 得到对象的所有属性,并拼接成一个字符串
var result = "";
for (var attr in theobj){
result += name + "." + attr + " = " +theobj[attr] + "<br>";
}
return result;
}
//-->
</script>
<body bgcolor="yellow"></body>
<script language = "javascript">
<!--
var wphone = new phone("王杰","010987654321", "北京市西城区");
document.write("<br><b>" + wphone.getattr(wphone, "wphone"));
//-->
</script>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -