工厂模式和构造函数模式

<!DOCTYPE HTML>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>工厂模式和构造函数模式</title>
</head>
<body>
<script type="text/javascript">
// function createObject(name,age){
// var obj=new Object();
// obj.name=name;
// obj.age=age;
// obj.run=function(){
// return this.name+this.age+'运行中';
// }
// return obj;
// }

// var box1=createObject('jack',100);
// var box2=createObject('lll',200);
// alert(box1.run());
// alert(box2.run());

function Box(name,age){
this.name=name;
this.age=age;
this.run=function(){
return this.name+this.age+'运行中';
};
}

var box=new Box('lll',100);
var box1=new Box('jack',200);
alert(box.run());
alert(box1.run());

var o=new Object();
Box.call(o,'yyy',300);
alert(o.run());

</script>
</body>
</html>

posted @ 2017-04-22 18:25  天--安静  阅读(390)  评论(0编辑  收藏  举报