(3)為什么是NaN?
var a = 100;
(function numbercalc(a) {
var b = a * 2;
var a = 200;
var c = a/2;
console.log("b "+ b); //NaN
console.log("c " + c);
})();
(4)遞歸地遍歷一個數(shù)組返回一個反向的字符串的函數(shù)
var reverseArray = function (x,indx,str) {
return indx == 0 ? str : reverseArray(x,--indx,(str+=" " + x[indx]));
};
var arr = ['apple','orange','grape','pear'];
console.log(reverseArray(arr,arr.length,""));
(5)fibonacci
var fibonacci = function (n) {
return n < 2 ? n : fibonacci(n-1) + fibonacci(n-2);
};
console.log(fibonacci(8));
返回的是輸入序號對應的數(shù)
(6)點擊刪除自身段落
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
html,body{margin: 0;padding: 0;}
</style>
</head>
<body>
<p>我是第1段</p>
<p>我是第2段</p>
<p>我是第3段</p>
<p>我是第4段</p>
<p>我是第5段</p>
<script>
var para = document.getElementsByTagName('p');
for (var i = 0;i < para.length;i++){
para[i].onclick = deleteSelf;
}
function deleteSelf() {
var parent = this.parentNode;
parent.removeChild(this);
alert("params" + document.getElementsByTagName('p').length);
}
</script>
</body>
</html>
(7)作用域this相關
window.val = 1;
var count = {
val:2,
getRes:function(){
this.val *= 2;
console.log(this.val);
val *= 2;
console.log(val);
console.log(this.val);
}
}
count.getRes();
console.log(window.val);
var count1 = count.getRes;
count1(); //2 4 8 8