函数
定义、调用、arguments
function test() {
console.log('hello world')
}
console.log(typeof test)
function add(a, b) {
return a + b
}
var addSum = function (x, y) {
return x + y
}
var addFun = new Function('a', 'b', 'return a+b')
console.log(add(1, 2))
console.log(addSum(1, 2))
console.log(addFun(1, 2))
console.log(test.__proto__ === Function.prototype)
console.log(test instanceof Object)
function Fun1() {
console.log('hello')
}
Fun1()
Fun1.call()
var obj = {
a: 1,
b: function () {
console.log(this.a)
},
}
obj.b()
;(function () {
console.log('立即执行')
})()
function Fun2() {
console.log('constructor function')
}
new Fun2()
function demo(a, b) {
console.log(arguments)
console.log(demo.length)
console.log(arguments.length)
console.log(arguments.callee == demo)
arguments[0] = 99
}
demo(1, 2, 3)
function maxValue() {
var flagNum = arguments[0]
for (var i = 0; i < arguments.length; i++) {
if (flagNum < arguments[i]) {
flagNum = arguments[i]
}
}
return flagNum
}
console.log(maxValue(2, 4, 3, 1, 6, 7, 4))
作用域、变量提升
var a = 'aaa'
function show() {
var b = 'bbb'
console.log(a)
}
show()
console.log(num1)
var num1 = 10
num2 = 10
num3 = 10
console.log(num3)
fun1()
function fun1() {
console.log('function one')
}
var fun2 = function () {
console.log('function two')
}
fun3()
function fun3(e) {
console.log(e)
}
预编译
function foo() {
var a = (b = 200)
}
foo()
console.log(b)
console.log(window.b)
console.log(window.a)
function baz(a) {
console.log(a)
var a = 666
console.log(a)
function a() {}
console.log(a)
function b() {}
console.log(b)
}
baz(1)
this 指向 + call/apply/bind
function fun1() {
console.log(this)
}
fun1()
setTimeout(function () {
console.log(this)
}, 0)
;(function () {
console.log(this)
})()
var obj = {
name: 'falcon',
showName() {
console.log(this)
},
}
obj.showName()
function fun2() {
console.log(this)
}
new fun2()
var obj = {
name: 'falcon',
age: 21,
}
function fun1(a, b) {
console.log(this)
console.log(this.name)
console.log(a + b)
}
fun1.call(obj, 1, 2)
function father(myName, myAge) {
this.name = myName
this.age = myAge
}
function son(myName, myAge) {
father.call(this, myName, myAge)
}
var son1 = new son('jerry', 18)
console.log(son1)
var obj1 = {
name: 'alice',
age: 23,
}
function fun2(a, b, c) {
console.log(this.name)
console.log(a + '+' + b + '+' + c + '=' + (a + b + c))
}
fun2.apply(obj1, [1, 2, 3])
var arr = [2, 4, 6, 3, 5, 7, 1]
console.log(Math.max.apply(null, arr))
console.log(Math.min.apply(null, arr))
var obj3 = {
id: 1,
lesson: 'Chinese',
}
function fun3(a, b) {
console.log(this.id)
console.log(a * b)
}
fun3.bind(obj3, 2, 3)()
Function.prototype.myCall = function (context, ...args) {
if (typeof this !== 'function') {
throw new TypeError('It must be a function')
}
if (!context) {
context = window
}
const fn = Symbol()
context[fn] = this
const result = context[fn](...args)
delete context[fn]
return result
}
fun1.myCall(obj, 1, 5)
Function.prototype.myApply = function (context, args = []) {
if (typeof this !== 'function') {
throw new TypeError('It must be a function')
}
if (!context) {
context = window
}
const fn = Symbol()
context[fn] = this
const result = context[fn](...args)
delete context[fn]
return result
}
console.log(Math.max.myApply(null, arr))
Function.prototype.myBind = function (context, ...args) {
const fn = this
if (typeof fn !== 'function') {
throw new TypeError('It must be a function')
}
if (!context) {
context = window
}
return function (...otherArgs) {
return fn.apply(context, [...args, ...otherArgs])
}
}
fun3.myBind(obj3, 3, 3)()
高阶函数+闭包
function fun1(a, b, callback) {
console.log(a + b)
callback && callback()
}
fun1(2, 3, function () {
console.log('callback function')
})
function fun2() {
var a = 20
return function () {
console.log(a)
}
}
var res = fun2()
res()