创建js对象的属性和方法

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

<!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>

原文地址:https://www.cnblogs.com/jinling/p/6207611.html