创建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>
<!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>
相关文章推荐
- js 创建对象、属性、方法
- js 创建对象、属性、方法
- JS的Object类的属性、方法及如何创建对象
- [荐]JS入门·对象属性方法大总结
- 显示js对象所有属性和方法的函数
- js中对象不支持此属性或方法
- 转"js入门·对象属性方法大总结"
- 前台JS脚本出现“Automation服务器不能创建对象“错误的解决方法
- js-window对象的方法和属性资料
- 显示js对象所有属性和方法的函数
- JS 创建对象(常见的几种方法)
- js-window对象的方法和属性资料
- js入门·对象属性方法大总结
- js入门·对象属性方法大总结
- js创建对象的几种方法
- js入门·对象属性方法大总结
- js入门·对象属性方法大总结
- js-window对象的方法和属性资料
- js 获取函数的所有参数和遍历JavaScript某个对象所有的属性名称和值的方法
- js创建对象的几种方法