重学前端

14.Function类型

2019-05-31  本文已影响0人  ChangLau

Function 类型

函数声明
// 函数声明
function sum(num1, num2){
    return num1 + num2
}

// 函数表达式
var sum = function(num1, num2){
    return num1 + num2
}
函数名是指针
function sum(num1, num2){
    return num1 + num2
}
console.log(sum(10, 10)) // 20

var anotherSum = sum
console.log(anotherSum(10, 10)) // 20

sum = null
console.log(anotherSum(10, 10)) // 20
函数没有重载
function addSomeNumber(num){
    return num + 100
}

// 函数名指针重新指向新的对象
function addSomeNumber(num){
    return num + 200
}

console.log(addSomeNumber(100)) // 300
函数声明和函数表达式的区别
console.log(sum(10, 10)) // 20
// 函数声明提升,在执行任何代码之前可用
function sum(num1, num2){
    return num1 + num2
}

console.log(sum(10, 10)) // sum is not a function
// 只有执行到所在代码行才会执行
var sum = function(num1, num2){
    return num1 + num2
}

作为值的函数

可以作为参数传入函数,或者作为函数的返回值

function callSomeFunction(someFunction, someArgument){
    return someFunction(someArgument)
}

function addTen(num){
    return num + 10
}

console.log(callSomeFunction(addTen, 10)); // 20
应用

数组对象根据某个对象属性排序

function compare(propertyName){
    return function(object1, object2){
        var value1 = object1[propertyName]
        var value2 = object2[propertyName]
        if(value1 < value2){
            return -1
        }else if(value1 > value2){
            return 1
        }else{
            return 0
        }
    }
}

var data = [{name:"ChangLau", age: 26}, {name:"Donbad", age: 25}]
console.log(data.sort(compare("age")))
// [{…}, {…}]
// 0: {name: "Donbad", age: 25}
// 1: {name: "ChangLau", age: 26}

console.log(data.sort(compare("name")))
// [{…}, {…}]
// 0: {name: "ChangLau", age: 26}
// 1: {name: "Donbad", age: 25}

函数内部属性

arguments

function factorial(num){
    if(num <= 1){
        return 1
    }else{
        return num * arguments.callee(num - 1)
    }
}

this

window.color = "red"
var o = { color : "blue" }

function showColor(){
    console.log(this.color);
}

console.log(showColor()); // "red"

o.showColor = showColor
// 函数执行环境对象
console.log(o.showColor()); // "blue"
函数对象属性 caller

保存着调用当前函数的引用(调用当前函数的引用,如果是在全局环境中调用值为 null)

function outer()}{
    inner()
}

function inner(){
    console.log(inner.caller)
}

outer()

// ƒ outer(){
//     inner()
// }

函数属性和方法

属性

length 属性表示函数希望接收的命名参数的个数。

function sayName(name){
    console.log(name);
}
console.log(sayName.length); // 1

function sum(num1, num2){
    return num1 + num2
}
console.log(sum.length); // 2

prototype 属性保存实例方法真正所在

function Person(name, age){
    this.name = name
    this.age = age
}
Person.prototype.showInfo = function(){
    console.log(this.name, this.age)
}
var p = new Person('ChangLau', 25)
p.showInfo() // ChangLau 25
方法

call() 和 apply()

修改函数赖以运行的作用域

var name = 'ChangLau'
var obj = {
    name: 'Donbad',
    age: 25
}
function showInfo(age){
    return this.name + ' ' + age
}

console.log(showInfo.call(window, 25)); // ChangLau 25
console.log(showInfo.call(obj, 25)); // Donbad 25

console.log(showInfo.apply(window, [25])); // ChangLau 25
console.log(showInfo.apply(obj, [25])); // Donbad 25

bind()

创建一个函数实例,其 this 值会被绑定到传给 bind()函数的值

var name = 'ChangLau'
var obj = {
    name: 'Donbad',
    age: 25
}
function showInfo(age){
    return this.name + ' ' + age
}

var showWindowInfo = showInfo.bind(window)
console.log(showWindowInfo(25)) // "ChangLau 25"
var showObjInfo = showInfo.bind(obj)
console.log(showObjInfo(25)); // "Donbad 25"
上一篇下一篇

猜你喜欢

热点阅读