1. 程式人生 > >js函數式編程

js函數式編程

結果 自身 his 原生 函數作用域 容易 goback javascrip 環境

  js 函數式編程

    定義把函數作為第一類對象是函數式編程的第一步,函數式編程是一種編碼風格,他通過書寫函數式代碼來解決問題(而不是一系列執行步驟,就像

    就像那種更主流的命令式編程),函數式編程可以讓代碼更容易測試、擴展、及模塊化

    1.函數是javascript中的一等公民,(主要是的某個變量可以等於一個函數,然後是用變量名加()去調用這個函數)

      1.1構造函數

      1.2普通函數

    1.1函數作為構造函數使用,一般構造函數的首字母大寫,做為構造函數使用,我們就可以使用new去創建那個實力

function Person() {
    
this.name = cz; this.age = 19; this.sex = ; this.fn = function (val) { console.log(val) } } let p1 = new Person(); console.log(p1.name,p1.age,p1.sex) p1.fn(a)

    1.2 作為普通函數調用,這個就是最簡單的使用

function speak(val) {
    console.log(val+wawawahahha);
}
speak(
duide)

    

  2幾種常見的函數使用

    2.1 函數作為變量的使用(回調函數,)

function goback(a,b,callback) {
    let c = a + b;
    callback(c)
}
function cc(last) {
    console.log(last)
}
goback(1,2,cc);

    /* 手動實現數組的排序功能 */

Array.prototype.zdysort = function (fn) {
    let len = this.length;
    for
(let i = 0;i < len; i++) { for(let j = i+1;j < len; j++) { let temp = this[i] if(fn(this[i],this[j])) { this[i] = this [j] this[j] = temp } } } } /* 升序 */ function sort(a,b) { return a>b } let arrys = [1,25,3,6,7,9,45,21,35,62,54,21] arrys.zdysort(sort) console.log(arrys) function usort(a,b) { return a<b } arrys.zdysort(usort) console.log(arrys) // (12) [1, 3, 6, 7, 9, 21, 21, 25, 35, 45, 54, 62] // (12) [62, 54, 45, 35, 25, 21, 21, 9, 7, 6, 3, 1]

    2.2函數柯裏化

      維基百科上說道:柯裏化,英語:Currying(果然是滿滿的英譯中的既視感),是把接受多個參數的函數變換成接受一個單一參數(最初函數的第一個參數)的函數,並且返回接受余下的參數而且返回結果的新函數的技術

    

/* 函數科裏化 */

/* 一個簡單的函數柯裏化 */

function add (a,b) {
    return a+b;
}
function kadd(a) {
    return function (b) {
        return a+b
    }
}
console.log(add(1,2)) // 3
console.log(kadd(1)(2))  // 3

/* 柯裏化 實現函數參數的復用 */

function Istype (type) {
    return function (obj) {
        return typeof(obj) === type
    }
}

let Isnumber = Istype(‘number‘)
console.log(Isnumber(1))
console.log(Isnumber(‘a‘))
console.log(Isnumber(4))

let isobject = Istype(‘object‘)
console.log(isobject({}))
console.log(isobject(‘a‘))
console.log(isobject(4))
/* 使用函數柯裏化實現 函數的bind */

Function.prototype.zdybind = function (content) {
    let self = this
    return function () {
        return self.call(content,arguments)
    }
}

function Animal(){    
    this.name = "Animal";    
    this.showName = function(){    
        console.log(this.name);    
    }    
} 

// Animal.showName();// 報錯 animal is not a function

let a1 = new Animal();
a1.showName() // "Animal"
a1.showName.call({name:‘czcz‘}) //czcz
/* 使用自定義的bing函數 */
a1.showName.zdybind({name:‘czcz‘}) // 輸出無
/* bind 相對於 call和apply 需要自己手動調用 */
a1.showName.zdybind({name:‘czcz‘})() //‘czcz‘  自定義的zdybind函數就實現了

  函數,不得不提的就是函數上原生就有的三個方法,bind,call,apply,他們都能改變函數的執行作用域(運行時的函數this指向)

  call 和 apply 基本上一樣,就是傳入的第二參數不一樣,一個傳入的參數用,call(self,a,b,c,d) bind(self,[a,b,c,d]) ,而bind和他們的區別就是bind創建後不會立即執行,上面已經實現

  函數式編程遠遠都不止這麽一點,上面只是一些平常會經常遇見的問題,函數運行時的執行環境也是非常重要的。直接調用,作為構造函數調用,箭頭函數,apply bing,call,之後的函數作用域

  ,es6 還對函數新增的一些擴展

  3.es6函數的擴展

    3.1 函數的默認參數問題

function speak(name = ‘cz‘) {
    console.log(`${name} i love you`)
}
speak()
speak(‘czkjjj‘)

    3.2參數的解構賦值

    3.3 rest 參數,直接將argument直接轉化成數組

    3.4name 和length屬性, name 代表函數名字, length 代表函數的參數 length 長度不包括rest 參數

    3.5 es6函數最重要的擴展,箭頭函數,(沒有自身this的函數,只想上一級不為箭頭函數的函數)

/* es6箭頭函數 */
/* 數組排序是箭頭函數的使用 */
let arrys = [25,1,34,5,64,15,3,1,7,84,65]
let newarrys = arrys.sort(function(a,b){
    return a - b
})
console.log(newarrys)
/* setTimeout 中的this指向  函數體內的this對象,就是定義時所在的對象 ,而不是運行時的this*/

let obj = {
    a: ‘czcz‘,
    b: ‘dddd‘,
    c: function () {
        setTimeout (function () {
            console.log(this.a)
        },0)
    }
}
obj.c(); // 輸出undefined 這裏內部的函數最終的執行環境 window
/* 改造一下函數 */
obj.d = function () {
    setTimeout(()=>
    {
        console.log(this.a)
    },0)
}
obj.d() // 輸出‘czcz‘ 這裏內部的函數最終的執行環境 obj

js函數式編程