美文网首页
关于函数function

关于函数function

作者: 青山白衣 | 来源:发表于2018-05-06 14:17 被阅读0次

函数的最后一句默认return undefined,写不写都会默认return undefined

function fn(x){
    console.log('接收到一个参数')
}
fn('dfsffd')//接收到一个参数
fn()//接收到一个参数
fn('')//接收到一个参数
fn(function(){})//接收到一个参数
function fn(x){
    if(typeof x!=='function'){
        console.log('接收到的参数不是函数')
        return false
    }else{
        console.log('接收到的是函数')
        return true
    }
}
fn('dfsffd')//接收到的参数不是函数 false
fn()//接收到的参数不是函数 false
fn('')//接收到的参数不是函数 false
fn(function(){})//接收到的是函数 true
function fn(x){
    if(typeof x!=='function'){
        console.log('接收到的参数不是函数')
        return false
    }else{
        console.log('接收到的是函数')
        x()//或x.call(),如果此处都不加(),则是把‘console.log('haha')’丢到内存中,并未执行它
        console.log('并且一句执行了该函数')
        return true
    }
}
fn(function(){console.log('haha')})
//是函数
//haha
//并且一句执行了该函数
//true
function fn(x){
    x(123)
}
fn(function(){
    console.log(arguments)
})
//Arguments [123, callee: ƒ, Symbol(Symbol.iterator): ƒ]
//0:123
//callee:ƒ ()
//length:1
//Symbol(Symbol.iterator):ƒ values()
//__proto__:Object

/*x就是
function(){
    console.log(arguments)
}
x的参数就是
function(){
    console.log(arguments)
}
的参数。arguments就是函数在执行时候接收到的参数*/

换个更直观的方法如下:

function fn(x){
    x(123)
}
fn(function(num){
    console.log(num)
})
//123

综上所述,做个foreach的小例子吧,如下:

//打印数组每项成员及其下标
function foreach(arr,x){
    for(i=0;i<arr.length;i++){
        x(arr[i],i)
    }
}
foreach(['apple','banana','pear'],function(value,key){
    console.log(value,key)
})
//apple 0
//1 banana 1
//pear 2

直接用数组的forEach方法实现如下:

(forEach接收1个函数,函数接2个参数,第一个参数是数组的value,第二个是数组的key)
var a = ['apple','banana','pear']
a.forEach(function(x,y){
    console.log(x,y)
})
//apple 0
//banana 1
//pear 2
a.forEach(function(){}) === a.forEach.call(a,function(){})
//true

实际上forEach是接收2个参数的,一个为自己,另一个为函数,如下例子帮助理解

var obj ={0:'a',1:'b',2:'c',length:'3'}
obj.forEach = function(x){
    for(i=0;i<obj.length;i++){
        x(this[i],i)//this 即为obj
    }
}
obj.forEach(function(value,key){
    console.log(value,key)
})
//a 0
//b 1
//c 2

相关文章

  • 关于函数function

    函数的最后一句默认return undefined,写不写都会默认return undefined 综上所述,做个...

  • GeekBand C++ WEEK3

    一 . 关于类型转换 1. conversion function (转换函数) —— “转出去” 类型转换函数的...

  • JS中的函数

    函数: function(){} 函数声明: function name(){ } 函数执行: name(); 函...

  • (6)神经网络算法(NN)

    1、关于非线性转化方程(non-linear transformation function) sigmoid函数...

  • shell函数工作实战应用

    shell函数:function函数定义:[function] funcname(){Statement;} 注意...

  • No.23 JavaScript函数

    一、函数的使用 1. 声明函数 // 声明函数function 函数名() {//函数体代码} function ...

  • 无标题文章

    关于非线性转化方程(non-linear transformation function) sigmoid函数(S...

  • 普通函数和箭头函数的this指向

    普通函数与箭头函数 普通函数指的是用 function 定义的函数: var hello = function()...

  • 初等函数及其图像

    初等函数是由幂函数(power function)、指数函数(exponential function)、对数函数...

  • Function

    函数定义 函数声明 function add () {} 函数表达式 var add = function () ...

网友评论

      本文标题:关于函数function

      本文链接:https://www.haomeiwen.com/subject/wblyrftx.html