第五章(4):Function

Function 類型

函數(shù)即對(duì)象,函數(shù)名即指針。每個(gè)函數(shù)都是Function類型的實(shí)例。函數(shù)名就是一個(gè)指向函數(shù)的指針。

創(chuàng)建函數(shù)

創(chuàng)建函數(shù)有三種方式方式。函數(shù)聲明,函數(shù)表達(dá)式和通過(guò)構(gòu)造函數(shù)創(chuàng)建。

/**
 * 三種創(chuàng)建函數(shù)的方式
 * */
// 函數(shù)聲明
function test1(x, y) {
  return x + y
}

// 函數(shù)表達(dá)式
var test2 = function (x, y) {
  return x + y
}

// 構(gòu)造函數(shù)
var test3 = new Function("x", "y", "return x + y")
console.log(test1(10, 20))  // 30
console.log(test2(10, 20))  // 30
console.log(test3(10, 20))  // 30
函數(shù)名即指針
/**
 * 函數(shù)即對(duì)象,函數(shù)名即指針
 */
function add(x, y) {
  console.log(x + y)
}
var calc = add
calc(1 , 2) // 3
calc = null   // 與函數(shù)斷絕連接
console.log(add(1, 2))  // 3
函數(shù)聲明和函數(shù)表達(dá)式

函數(shù)聲明會(huì)提前,可以在函數(shù)聲明就調(diào)用。函數(shù)表達(dá)式不會(huì)提前。必須在函數(shù)表達(dá)式聲明之后才可以調(diào)用。工作中一般都是先聲明,后調(diào)用。

/**
 * 函數(shù)聲明會(huì)提前,而函數(shù)表達(dá)式不會(huì)
 */
getName('李淳罡')  // 在函數(shù)聲明之前調(diào)用,可以執(zhí)行。結(jié)果為 天不生我李淳罡 劍道萬(wàn)古長(zhǎng)如夜
function getName(name) {
  console.log('天不生我' + name, '劍道萬(wàn)古長(zhǎng)如夜')
}

//  getSay('來(lái),給少爺上酒') // 函數(shù)表達(dá)式不會(huì)提前。結(jié)果為  Uncaught TypeError: getSay is not a function
var getSay = function (say) {
  console.log(say)
}
getSay('來(lái),給少爺上酒') // 來(lái),給少爺上酒
作為值的函數(shù)

函數(shù)名本身就是一個(gè)變量,所以也可以作為值來(lái)使用。

/**
 * 作為值的函數(shù)
 */
function rank(key) {
  return function (pre, next) {
    var preValue = pre[key]
    var nextValue = next[key]
    return preValue - nextValue
  }
}

var data = [
  {name: '王仙芝',comment: '以力證道',id: 2},
  {name: '洪洗象',comment: '半入云間',id: 1},
  {name: ' 鄧太阿',comment: '桃花劍神 ',id: 4},
  {name: '李淳罡',comment: '劍開天門',id: 3}
]

data.sort(rank('id'))
console.log(JSON.stringify(data)) // [{"name":"洪洗象","comment":"半入云間","id":1},{"name":"王仙芝","comment":"以力證道","id":2},{"name":"李淳罡","comment":"劍開天門","id":3},{"name":" 鄧太阿","comment":"桃花劍神 ","id":4}]

this

this引用的是執(zhí)行環(huán)境的對(duì)象。誰(shuí)調(diào)用它,它就指向誰(shuí)。函數(shù)的調(diào)用方式有以下幾種。

// 普通函數(shù)
var source = 'window'
function getSource() {
  console.log(this.source)
}
getSource() // window (在全局環(huán)境中調(diào)用,所以this指向window)
// 方法
var userName = '軒轅大磬'
var person = {
  userName: '軒轅敬城',
  say: function () {
    console.log(this.userName + '請(qǐng)老祖宗赴死')
  }
}
person.say()  // 軒轅敬城請(qǐng)老祖宗赴死 (this指向person)
var say = person.say  
console.log(say()) // 軒轅大磬請(qǐng)老祖宗赴死 (this指向window)
//構(gòu)造函數(shù)
function Biography(name) {
  this.name = name
}
let bg = new Biography('溫不勝')
console.log(bg.name)  // 溫不勝
call()和apply()

這兩個(gè)方法可以改變this的指向?qū)ο蟆?/p>

/**
 * call() apply()
 * 可以改變this的指向。
 * call的第一個(gè)參數(shù)是運(yùn)行函數(shù)的作用域,后面的參數(shù)是要傳遞的參數(shù)
 * apply的第一個(gè)參數(shù)是運(yùn)行函數(shù)的作用域,第二個(gè)參數(shù)是一個(gè)數(shù)組。(數(shù)組參數(shù))
 */

var name = 'window'
var obj1 = {
  name: 'obj1'
}

var obj2 = {
  name: 'obj2'
}

function fun(msg) {
  console.log(this.name, ' ', msg)
}

fun('hello')  // window   hello
fun.call(obj1, 'hello') // obj1   hello
fun.apply(obj2, ['hello'])  // obj1   hello
bind

bind方法可以綁定函數(shù)的作用域

/**
 * bind 可以綁定this的指向
 */
color = 'red'
var obj3 = {
  color: 'yellow'
}

function getColor() {
  console.log(this.color)
}

var getC = getColor.bind(obj3)
getC()  // yellow

引用

javascript高級(jí)程序設(shè)計(jì)

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容