97 lines
2.6 KiB
HTML
97 lines
2.6 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>
|
||
</head>
|
||
<body>
|
||
<script>
|
||
// 递归 函数内部调用他自己 必须有个终点
|
||
let num = 0;
|
||
function a(index){
|
||
if(index == 0){
|
||
return 0;
|
||
}else {
|
||
return index + a(index - 1)
|
||
//10+a(9)
|
||
//10+9+a(8)
|
||
//10+9+8+a(7)
|
||
//.....
|
||
//10+9+8+7+......+3+2+1+0
|
||
|
||
|
||
}
|
||
}
|
||
num = a(10)
|
||
alert(num)
|
||
|
||
// function a (){
|
||
// let b = 100;
|
||
// return function (){
|
||
// b++;
|
||
// return b;
|
||
// }
|
||
// }
|
||
// let c = a()
|
||
// alert(c())
|
||
// alert(c())
|
||
// alert(c())
|
||
// alert(c())
|
||
// alert(c())
|
||
|
||
function a (){
|
||
let name = "123"
|
||
var shop = {
|
||
a: "商品c",
|
||
b: "商品d"
|
||
}
|
||
return {
|
||
name: "321",
|
||
getname(name){
|
||
return "商品" + name +"的值是" + shop[name]
|
||
},
|
||
addshop(name1,item){
|
||
shop[item] = name1 // shop['pingguo']="苹果"
|
||
alert(this.name) //543
|
||
},
|
||
getlist(){
|
||
return shop
|
||
},
|
||
setname(name){
|
||
this.name = name
|
||
}
|
||
}
|
||
}
|
||
let sa = a()
|
||
alert(sa.getname('a')) //商品a的值是商品c
|
||
sa.setname("543") // sa.name = 543
|
||
sa.addshop("苹果","pingguo") //543
|
||
alert(sa.getname('pingguo')) //商品pingguo的值是、苹果
|
||
|
||
console.log(sa.getlist())
|
||
function b (){
|
||
var shop = {
|
||
a: "商品a",
|
||
b: "商品b"
|
||
}
|
||
return {
|
||
getname(name){
|
||
return "商品" + name +"的值是" + shop[name]
|
||
},
|
||
addshop(name,item){
|
||
shop[item] = name
|
||
},
|
||
getlist(){
|
||
return shop
|
||
}
|
||
}
|
||
}
|
||
let sb = b()
|
||
|
||
// 递归 生成杨辉三角
|
||
// 闭包 封装一个累加器 调用add 方法+1 调用get方法 获取现在的累加值 ‘当前值是 ’
|
||
</script>
|
||
</body>
|
||
</html> |