<html>
<head>
<head>
<body>
<script language="javascript">
//对象简单实例
function Person(){}
var person1 = new Person();
person1.age = 22;
person1.name = "wjp";
//alert(person1.name + ":" +person1.age);
function sayFunc(){
//alert(person1.name + ":" +person1.age);
var x = "name" ;
eval("alert(person1." + x + ");"); // 等价于alert(person1.name);
//alert(person1["age"] + ":" +person1[x]);
x = "age" ;
//alert(person1["age"] + ":" +person1[x]);
//alert(person1["age"] + ":" +person1["name"]);
}
person1.say = sayFunc();
alert(person1.say);
//person1.say();
person1.say = sayFunc;
alert(person1.say);
person1.say();
var person2 = new Person();
// 提示 undefined
alert(person2.age);
</script>
</body>
</html>
JavaScript - 对象实例化、调用对象的属性及方法
最新推荐文章于 2023-02-03 20:24:40 发布