变量作用域
var a = 1
function test() {
var a = 2
console.log(a) // 2
}
test()
var a = 1
function test2() {
console.log(a) // undefined
var a = 2
}
test2()
var a = 1
function test3() {
console.log(a) // 1
a = 2
}
test3()
let b = 1
function test4() {
console.log(b) // b is not defined
let b = 2
}
test4()
function test5() {
let a = 1
console.log(a) // 1
{
let a = 2
}
}
test5()
函数参数
function test6() {
console.log(arguments) // [1,2]
}
test6(1,2)
function test7 () {
return function () {
console.log(arguments) // 未执行到此,无输出
}
}
test7(1,2)
function test8 () {
return function () {
console.log(arguments) // [3,4]
}
}
var fn = test8(1,2)
fn(3,4)
var args = [1,2]
function test9() {
console.log(arguments) // [1,2,1,2]
}
Array.prototype.push.call(args,2)
test9(...args)
回调函数
function test10 (name,fn) {
console.log(name) // 'luozh'
console.log(fn()) // 'zhuyj'
}
test10('luozh',function() {
return 'zhuyj'
})
对象拷贝与赋值
var obj = {
name: 'luozh',age: 23
}
var newObj = obj
newObj.name = 'zhuyj'
console.log(obj) // Object {name: "zhuyj",age: 23}
console.log(newObj) // Object {name: "zhuyj",age: 23}
var obj2 = {
name: 'luozh',age: 23
}
var newObj2 = Object.assign({},obj2,{color: 'blue'})
newObj2.name = 'zhuyj'
console.log(obj2) // Object {name: "luozh",age: 23}
console.log(newObj2) // Object {name: "zhuyj",age: 23,color: 'blue'}
var obj3 = {
name: 'luozh',age: 23
}
var newObj3 = Object.create(obj3)
console.log(obj3) // Object {name: "luozh",age: 23}
console.log(newObj3.proto) // Object {name: "luozh",age: 23}