53 lines
1.4 KiB
HTML
53 lines
1.4 KiB
HTML
|
<!DOCTYPE html>
|
||
|
<html lang="en">
|
||
|
<head>
|
||
|
<meta charset="UTF-8">
|
||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||
|
<title>Document</title>
|
||
|
<script>
|
||
|
// var obj={}// 定义一个对象 字面量
|
||
|
var obj =new Object() // 构造函数
|
||
|
// var order=new Number(3)
|
||
|
obj.name="暖暖" //增加一个属性
|
||
|
obj.age=19
|
||
|
obj.color="pink"
|
||
|
obj.say=function(e){ // 增加一个方法
|
||
|
console.log(e)
|
||
|
}
|
||
|
console.log(obj)
|
||
|
obj.say("我是暖暖") //对象里面方法调用
|
||
|
function changeage(){
|
||
|
obj.age=20;
|
||
|
console.log(obj,"new")
|
||
|
}
|
||
|
function del(){
|
||
|
delete obj.age // 删除属性
|
||
|
console.log(obj)
|
||
|
}
|
||
|
|
||
|
|
||
|
// 自定义一个 构造函数
|
||
|
function Person(name,age,color){
|
||
|
this.name=name
|
||
|
this.age=age
|
||
|
this.color=color
|
||
|
this.sayhi=function(){
|
||
|
console.log('hi')
|
||
|
}
|
||
|
}
|
||
|
|
||
|
var nuan=new Person('暖暖',20,'pink')
|
||
|
console.log('we' in nuan)
|
||
|
console.log('name' in nuan) // in 前面一定是属性的名字
|
||
|
// for in 遍历对象
|
||
|
for(let i in nuan){
|
||
|
console.log(i,nuan[i]) //i 是属性名 nuan[i] 属性值
|
||
|
}
|
||
|
|
||
|
</script>
|
||
|
</head>
|
||
|
<body>
|
||
|
<button onclick="changeage()">change</button>
|
||
|
<button onclick="del()">del</button>
|
||
|
</body>
|
||
|
</html>
|