美文网首页
ES6(三) 函数 1

ES6(三) 函数 1

作者: 蒋小花_4b6c | 来源:发表于2020-05-06 12:51 被阅读0次

1.带参数默认值的函数

2.arguments

3.形参默认值,对arguments的影响

4.参数默认值表达式

5.

带参数默认值的函数

// ES5

function makeRequest(url, timeout, callback) {

    timeout = timeout || 2000;

    callback = callback || function() {};

    // 函数的剩余部分

}

// ES6

function makeRequest(url, time = 2000, callback = function() {} ) {

    // 函数的剩余部分

}

// 如果第二个参数是可选的,第三个参数是必填的,

// 那么可将第二个参数赋值为undefined/null

function makeRequest(url, time = 2000, callBack) {

    console.log(url, time = 2000, callBack);

}

makeRequest('/foo', undefined, function aa() { console.log('this is callBackFun.') });

            // 输出: /foo 2000 ƒ aa() { console.log('this is callBackFun.') }

makeRequest('/foo', null, function aa() { console.log('this is callBackFun.') });

            // 输出:  /foo 2000 ƒ aa() { console.log('this is callBackFun.') }

arguments

它是JS的一个内置对象.    

JS不像JAVA是显示传递参数,JS传的是形参,可以传也可以不传.

若方法里没有写参数却传入了参数,该如何拿到参数呢,答案就是arguments了.

function getParam() {

    console.log(arguments);

    console.log(typeof arguments );

    console.log(arguments.length );

    const newArgments = Array.prototype.slice.call(arguments);

    // 把arguments转化成一般的数组

    newArgments.forEach(element => {

        console.log(element);

    });

}

getParam();

getParam([1, 2]);

getParam([1, 2], 'time', 'url');

形参默认值,对arguments的影响

// ES5 的非严格模式:

function mixArgs(first, second) {

    console.log(first === arguments[0]);

    console.log(second === arguments[1]);

    first = "c";

    second = "d";

    console.log(first === arguments[0]);

    console.log(second === arguments[1]);

}

mixArgs("a", "b");

// 输出: true true true true

// 更改 first 与 second 改变了 arguments 对象

// ES5 的严格模式:

function mixArgs(first, second) {

    // "use strict";

    console.log(first === arguments[0]);

    console.log(second === arguments[1]);

    first = "c";

    second = "d"

    console.log(first === arguments[0]);

    console.log(second === arguments[1]);

}

mixArgs("a", "b");

// 输出: true true false false

// 更改 first 与 second 就不会再影响 arguments 对象

ES6 与 ES5严格模式 一样 

// 非严格模式

function mixArgs(first, second = "b") {

    console.log(arguments.length);

    console.log(first === arguments[0]);

    console.log(second === arguments[1]);

    first = "c";

    second = "d"

    console.log(first === arguments[0]);

    console.log(second === arguments[1]);

}

mixArgs("a");

// 输出: 1 true false false false

参数默认值表达式

参数默认值并不要求一定是基本类型的值(int, string array, object……)

function getValue() {

    return 5;

}

function add( first,  second = getValue() ) {

    return first + second;

}

console.log( add(1, 1) ); // 2

console.log( add(1) ); // 6

如果getValue() 是一个而可变的数,情况会怎样?

let value = 5;

function getValue() {

    return value++;

}

function add( first, second = getValue() ) {

    return first + second;

}

console.log( add(1, 1) ); // 2

console.log( add(1) );  // 6

console.log( add(1) ); // 7

console.log( add(1) ); // 8

Tips:

second = getValue(),注意此处是带有括号的,否则,没有括号就是引用.

另一种有趣的能力:可以将前面的参数作为后面参数的默认值

function add( first, second = first ) {

    return first + second;

}

console.log( add(1, 1) ); // 2

console.log( add(1) ); // 2

将两种方法结合:

第二个参数的默认值是一个函数返回的值,第二个参数的函数使用了第一个参数

function getValue(value) {

    return value + 5;

}

function add( first,  second = getValue(first) ) {

    return first + second;

}

console.log( add(1, 1) ); // 2

console.log( add(1) ); // 7

但是,注意啦!!

引用其他参数来为参数进行默认赋值时,仅允许引用前方的参数,因此前面的参数不能访问 后面的参数。

(默认值参数可以使用前面的参数,不能使用后面的参数.)

function getValue(value) {

    return value + 5;

}

function add(first = second, second) {

    return first + second;

}

console.log( add(1, 1) ); // 2

console.log( add(1) ); // NAN

console.log( add(1, 0) ); // 1

console.log( add(1, undefined) ); // NAN

console.log( add(undefined, 1) ); // 抛出错误

此处的无法访问后面的参数,就要查看“暂时性死区”这一概念.

相关文章

  • ES6(三) 函数 1

    1.带参数默认值的函数 2.arguments 3.形参默认值,对arguments的影响 4.参数默认值表达式 ...

  • JavaScript学习笔记-函数

    一 函数定义 1 方式一 在JavaScript中,定义函数的方式如下: 2 方式二 3 方式三(重要) ES6标...

  • es6的数值,函数,字符串扩展

    一、es6的数值扩展 二、es6的函数扩展 代码效果:对象解构在函数中的应用 三、es6字符串扩展 常用方法 代码...

  • ES6&函数扩展

    ES6函数的扩展 1.函数默认值 定义:ES6允许为函数设定默认值,即直接写在参数定义的后面 示例function...

  • ES6新特性4:函数的扩展

    1:函数参数的默认值 在ES6之前不能直接为函数设置默认值,只能在函数内单独判断处理。ES6可以为函数设置默认值。...

  • 关于ES6语法基础讲解(函数的写法)-3

    ES5的函数写法: function arr(){ var a = 1; } ES6的函数写法: ()=>{}...

  • ES6箭头函数简介

    @(JS技巧)[ES6|箭头函数] ES6箭头函数(Arrow Functions) ES6可以使用“箭头”(=>...

  • Es6基础语法

    ES6 函数 1、this :this代表当前正在执行的对象 2、箭头函数(1)箭头函数的this是在定义的时候...

  • ES6函数的扩展(一)

    参考:ES6入门(阮一峰) 一、ES6为函数设置默认参数 ES6之前不能为函数设置默认参数,ES6新增可以为函数设...

  • 29.JavaScript-箭头函数

    1.什么是箭头函数? 箭头函数是ES6中新增的一种定义函数的格式目的:就是为了简化定义函数的代码 2. 在ES6之...

网友评论

      本文标题:ES6(三) 函数 1

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