//js是通过function来封装自定义对象的。
function perMathod(name,age){
this.name = name;
this.age = age;
this.getName = function(){
return this.name;
}
this.setName = function(name){
this.name = name;
}
this.show = function(){
alert(this.name+"--"+this.age);
}
}
//调用创建的的自定义对象,通过方法new来实现。
var p = new perMathod("张三丰",99);
console.log(p.name+"::"+p.age);
p.setName("太极张三丰");
console.log(p.getName());
p.name = "赵敏";
p.age = 38;
p.show();
var cat = {
colorData:'红色',
age : 12,
name : "汤姆",
show: function(){
alert(this.colorData+this.age+this.name);
}
};
console.log(cat.name+"--"+cat.age+"--"+cat.colorData);
cat.show();
cat.date = "07-10";
console.log(cat)JavaScript--基础(对象的几种使用方式)
最新推荐文章于 2022-01-29 15:14:30 发布
本文介绍JavaScript中如何使用function构造函数创建自定义对象,并为这些对象添加属性及方法。示例包括设置和获取对象属性的具体实现。
618

被折叠的 条评论
为什么被折叠?



