5种:
1:直接创建,
var zhangsan=new Object();
zhangsan.name=“张三”;
zhangsan.phone=1234567;
zhangsan.walk=function(){
console.log(this.name+“正在散步。。。。”);
}
2:对象初始化,
var tercher={
name:“aaa”,
age:18,
sleep:function(){
console.log(this.name+“正在睡觉”);
}
}
tercher.sleep();
3:构造函数,
function Student(age){
this.name=“张三”;
this.age=age;
this.walk=function(){
console.log(this.name+“正在散步。。。。”);
}
}
var student=new Student(21);
4:prototype原型方式,
function Student(){
}
Student.prototype.name=“王尉行”;
Student.prototype.age=12;
Student.prototype.dohomework=function(){
console.log(Student.prototype.name+“正在学习”);
}
5:混合构造‘
function Student(name){
this.name=name;
this.age=12;
}
Student.prototype.sleep=function(){
console.log(this.name+“正在睡觉。。。。。。”);
}
var student=new Student(“王尉行”);
student.sleep();
轮播,举例:
Javascript 5种自定义对象方法
最新推荐文章于 2022-06-29 22:11:21 发布