<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>javascript028.html</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
</head>
<body>
<!--
自定义类添加,修改,删除属性和方法。
-->
<script type="text/javascript">
//自定义类Person
function Person()
{
}
//创建对象
var p = new Person();
//alert(typeof p); //object
//1:添加属性
//alert(p.name+"***"+p.age+"***"+p.sex);
p.name = "林青霞";
p.age = 26;
p.sex = "女";
//alert(p.name+"***"+p.age+"***"+p.sex);
//这里创建对象后,添加的属性只是给每个对象自己特有的值
//var p2 = new Person();
//p2.name = "石松";
//p2.age = 28;
//p2.sex = "男";
//alert(p2.name+"***"+p2.age+"***"+p2.sex);
//2:添加方法
/*
function show()
{
alert(p.name+"***"+p.age+"***"+p.sex);
}
//函数赋值
p.start = show;
//调用
p.start();
*/
//匿名函数
p.start = function()
{
alert(p.name+"***"+p.age+"***"+p.sex);
};
//调用
//p.start();
//3:修改属性和方法
//alert(p.name+"***"+p.age+"***"+p.sex);
//p.name = "刘德华";
//alert(p.name+"***"+p.age+"***"+p.sex);
/*
p.start = function()
{
alert("面向对象的js,其实也不过如此");
}
p.start();
*/
//4:删除属性和方法
p.name = undefined;
p.start = undefined;
//alert(p.name+"***"+p.age+"***"+p.sex);
//p.start();
</script>
</body>
</html>
JavaScript基础(17)—自定义类添加属性和方法
最新推荐文章于 2021-10-24 17:35:29 发布