筆記:
第一種:原型法
let obj = new Object();
obj.id = 1;
obj.name = "路人甲";
obj.age = "22";
obj.hello = function(){
document.write("測試方法")
}
obj.add = function(x,y){
return x + y;
}
obj.hello();
document.write(obj.add(5,6));
document.write(obj.id);
document.write(obj.name);
document.write(obj.age);
第二種:常量法(最常見)
let user={
id:3,
name:"路人丙",
age:23,
hello:function(){
document.write("測試方法柠硕!");
},
add:function(x,y){
return x + y;
},
myminus:minus
}
function minus(x,y){ return x-y}
document.write(user.id + " " + user.name + " " + user.age + " ");
user.hello();
document.write(user.add(5,6));
document.write(user.myminus(10,20));
第三種:構(gòu)造函數(shù)法
function user(id,name,age){
this.id = id;
this.name = name;
this.age = age;
this.hello = function(){
document.write("ceshi");
}
this.Myminus = minus;
}
function minus(x,y){
return x - y;
}
let u = new user(2,"路人丁",22);
document.write(u.id + u.name + u.age);
u.hello();
document.write(u.Myminus(20,30));