1 var a = b = 1; infunctionWrite to causes global leakage of variablesb/ / hang onwindowUpper 2 Fibonacci sequence // 1 1 2 3 5 8 13 21 //n1 n2 n3
// n1 n2 n3
// n1 n2 n3
// n1 n2 n3
let n1 = 1;
let n2 = 1;
let n3;
let n = Number(window.prompt('Please enter n valid value'))
if (n <= 0) {
alert('undifiend')}else {
if (n <= 2) {
n1 = n2 = 1
}
for (let i = 0; i < n; i++) {
n3 = n1 + n2;
n1 = n2;
n2 = n3;
}
console.log(n3)
}
3Function expressions (anonymous function expressions/function literals)let test = function test1(){
let a = 1,b = 2;
cosole.log(a, b)
test1() / / closures
}
test1() // is not defined is ignored in external calls
4 funtion test(a,b){
cosole.log(test.length) // The length of the parameter is 2
cosole.log(arguments.length) // The argument length is 3
}
test(1.2.3)
5If an argument is passed a value, the parameter can change its value. If an argument is not passed a value, the parameter can change its valueundefined
var a =1
function test(a,b){
console.log(a)
console.log(b)
a = 2;
b = 22;
/ / a and the arguments [0]
console.log(a, arguments[0])
console.log(b, arguments[1])
}
test(a)
console.log(a)
Copy the code