您的位置:首页 > Web前端 > JavaScript

创建js对象的属性和方法

2016-12-21 14:32 267 查看
按照如下的创建对象的方法,可以节省内存。记录一下方便日后使用

<!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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: