<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>js对象的创建3</title>
<meta name="author" content="ASUS" />
<!-- Date: 2014-11-13 -->
<script type="text/javascript">
/*
*通过工厂的方式来创建对象
* 在createObject方法中创建一个对象
* 然后给该对象设置相应的属性和方法
* 最好返回该对象
*/
function createObject(name,age){
var obj = new Object();
obj.name=name;
obj.age=age;
obj.sayHello = function(){
alert(this.name+","+this.age);
}
return obj;
}
/*
*使用工厂的方式 有效的解决了重复利用的问题
* 同时也带来了新的问题 不能通过instanceof来检测p1和p2的类型
* 通过typeof只能检测出师object类型
*/
var p1 = createObject("aa",20);
var p2 = createObject("bb",22);
</script>
</head>
<body>
</body>
</html>
"http://www.w3.org/TR/html4/strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>js对象的创建3</title>
<meta name="author" content="ASUS" />
<!-- Date: 2014-11-13 -->
<script type="text/javascript">
/*
*通过工厂的方式来创建对象
* 在createObject方法中创建一个对象
* 然后给该对象设置相应的属性和方法
* 最好返回该对象
*/
function createObject(name,age){
var obj = new Object();
obj.name=name;
obj.age=age;
obj.sayHello = function(){
alert(this.name+","+this.age);
}
return obj;
}
/*
*使用工厂的方式 有效的解决了重复利用的问题
* 同时也带来了新的问题 不能通过instanceof来检测p1和p2的类型
* 通过typeof只能检测出师object类型
*/
var p1 = createObject("aa",20);
var p2 = createObject("bb",22);
</script>
</head>
<body>
</body>
</html>