function 构造函数([参数列表]){
this.属性1=属性值;
...;
this.属性N=属性值;
this.方法1=function([参数列表]){
方法体
};
...;
this.方法N=function([参数列表]){
方法体
};
}
var 对象名 = new 构造函数([参数列表]);
function Student(name){
this.name=name;
this.eatting=function(){
console.log(this.name+"正在吃东西");
};
}
var stu = new Student("Lily");
stu.eatting();
4.原型式
function 对象创建器(){
};
对象创建器.prototype.属性=属性值;
对象创建器.prototype.方法=function([参数列表]){
方法体
};
var 对象名 = new 对象创建器([参数列表]);
function Student(){
}
Student.prototype.name="Kitty";
Student.prototype.eatting=function(){
console.log(this.name+"正在吃东西");
};
var stu = new Student();
stu.eatting();
5.混合式
function 构造函数([参数列表]){
this.属性1=属性值;
...;
this.属性N=属性值;
};
}
对象创建器.prototype.方法=function([参数列表]){
方法体
};
var 对象名 = new 构造函数([参数列表]);
function Student(name){
this.name=name;
}
Student.prototype.eatting=function(){
console.log(this.name+"正在吃东西");
};
var stu = new Student("Linda");
stu.eatting();
var 对象变量名={属性1:属性值1,...,属性N:属性值N,方法1:function([参数列表]){方法体},...,方法N:function([参数列表]){方法体name:"Tim", //注意属性的定义用:age:12,console.log(this.name+"正在吃东西");},console.log(this.name+"正在跑步");