javascript定义对象的方式
javascript 定义对象的方式,有6 种:
1.1在已有对象上添加属性
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>New Web Project</title>
</head>
<script type="text/javascript">
//同过已有对象,扩充属性
var object =new Object();
object.name="zhangsan";
object.sayName=function(name){
this.name=name;
alert(this.name);
}
object.sayName("lisi");
</script>
</html>
1.2 工厂方式创建对象
<script type="text/javascript">
//工厂方式 创建对象
function createObject(){
var object = new Object();
object.username = "hw";
object.password = "810068";
object.get = function(){
alert(this.username + " , " + this.password);
}
return object;
}
var object1 = createObject();
var object2 = createObject();
object1.get();
</script>
<script type="text/javascript">
//工厂方式 创建对象
function createObject(name){
var object = new Object();
object.username = name;
object.password = "81";
object.get = function(){
alert(this.username + " , " + this.password);
}
return object;
}
var object1 =
- 发表评论
-
- 最新评论 更多>>