金玲

导航

 

 

按照如下的创建对象的方法,可以节省内存。记录一下方便日后使用

<!Doctype html>
<html>
<head>
<title></title>
</head>
<body>
<script>
var Person = function(name,age){
this.name = name;
this.age = age;
};
Person.prototype={
setName:function(name){
this.name = name;
return this
},
setAge:function(age){
this.age = age;
return this
},
getName:function(){
return this.name;
},
getAge:function(){
return this.age;
}
};

var p = new Person();
p.setName("lili").setAge("11");
console.info(p.getName());
console.info(p.getAge());

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

posted on 2016-12-21 14:32  金_玲  阅读(237)  评论(0编辑  收藏  举报