webveuje/teaching/lhj/kejian/js/objectcreate.html
2021-05-11 11:33:55 +08:00

73 lines
1.9 KiB
HTML

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script>
// 创建对象
// 方法一 字面量创建对象
// var obj={name:"小明"}
// 方法二 工厂模式
// obj{
// size:100,
// height:30,
// width:50,
// weight:'1t'
// }
function creator(size,height,width,weight,funname,fun){
var newobj={}
newobj.size=size;
newobj.height=height;
newobj.width=width;
newobj.weight=weight;
newobj.jisuan=function(){
var mian=this.width*this.height
if(mian==this.size){
console.log("success")
}else{
console.log("error")
}
};
if(funname){
newobj[funname]=fun
}
return newobj
}
var obj1=creator(100,10,10,"1g","sayhello",function(){
console.log("hello")
})
// console.log(obj1)
var obj2=creator(100,30,50,'1t',"fy",123)
// 方法三 构造函数
function create(uname,pwd){
this.username=uname;
this.pwd=pwd;
this.login=function(){
console.log("登录成功")
}
}
var player1=new create("刘能","123")
var player2=new create("谢广坤","456")
var objj={
like:{
name:"aaa",
waihao:{
name1:"qwe"
}
}
}
console.log(objj['like']['waihao']['name1']);
</script>
</head>
<body>
</body>
</html>