null
console.log(typeof null) //表示 為 無(wú)對(duì)象 0
undefined
console.log(typeof undefined) //表示 無(wú)的原始值 NaN
以下4種為undefined 的情況
let o;
console.log(o)
let obj = {}
console.log(obj.a)
function fn(a,b){
console.log(a,b)
}
fn(4)
function abcd(){
console.log('111')
}
console.log(abcd())
foreach
1、沒(méi)有返回值
2箩做、不能用break打斷
3莽红、遍歷的是value
let arr = ['a','b','c']
let res = arr.forEach(element =>{
console.log(element)
// break;
return element+'1'
})
map
1、有返回值(數(shù)組) 默認(rèn) return 是 undefined
2邦邦、接受的參數(shù)是一個(gè)函數(shù) (key value)
3安吁、不能用break打斷
let arr = ['a','b','c']
let res = arr.map(value,key){
//break;
return value+'1'
}
js 遞歸求和1-100
function add(num1,num2){
let num = num1+num2
if(num2+1>100){
return num
}else{
return add(num,num2+1)
}
}
let sum = add(1,2)
console.log(sum) //5050
雙向數(shù)據(jù)綁定
<inupt placeholder='請(qǐng)輸入名字' id='username'>
顯示值:<p id='uName'></p>
<script>
let obj = {}
Object.defineProperty(obj,'username',{
//取值
get: function(){
console.log('取值')
},
set: function(){
console.log('設(shè)置值')
document.getElementById('uName').innerText = val
},
})
document.getElementById('username').addEventListener('keyup',function(){
//event
obj.username = event.target.value
})
</script>