方法:
function
省略并在参数後面加上=>
。{}
及return
去掉并改成。()
都可以省略。 const family = function (params) {return console.log('函式的参数: ' + params)}
family(10); //函式的参数: 10
const family03 = (params) => {return console.log('函式的参数: ' + params)}
family03(30); //函式的参数: 30
const family02 = (params) => console.log('函式的参数: ' + params);
family02(20); //函式的参数: 20
PS1:如果依照上述第2点规则,且回传的值是物件实字,则会出现undefined,需要用({})
包裹。
const fn = function () {
return {
data: 1,
name: 'TOM',
};
};
console.log(fn()); //{data: 1, name: 'TOM'}
const arrowfn = () => {};
console.log(arrowfn()); //undefined
const arrowfn01 = () => ({});
console.log(arrowfn01()); //{}
**PS2:**如果依照上述第2点规则,箭头函式搭配判断子,则会报错,依样需要用()
包裹。
let number = 0;
const numberFn = number || function (param) { console.log(param) };
numberFn(100); //100
let number01 = 0;
const numberFn01 = number01 || ((param) => { console.log(param) });
numberFn01(200); //200
1.缺乏内建arguments:传统函式会内建argument
,可以把传入函式的参数,全部以类似阵列的形式呈现出来;但是箭头函式无法,必须在参数内输入...变数
(称为其余参数),才可以。
2.this
3.箭头函式无法当作建构函式
const family = function () {
return console.log(arguments)
}
family(10, 20, 30); //Arguments(3) [10, 20, 30, callee: ƒ, Symbol(Symbol.iterator): ƒ]
const family = () => {
return console.log(arguments)
}
family(10, 20, 30); //Uncaught ReferenceError: arguments is not defined
const family = (...varible) => {
return console.log(varible)
}
family(10, 20, 30); //(3) [10, 20, 30]
1.没有arguments
2.this:
传统函式的this:与this如何被呼叫有关。
箭头函式的this:依据所属的函式在哪里建立而决定。
call、apply、bind
失效:无法指定this的指向。
3.箭头函式无法当作建构函式
const fn = function () { // 传统 function 的写法
console.log(this);
};
fn(); // Window Object
const arrowFn = () => { //arrow Function在全域中建立
console.log(this);
};
arrowFn(); // Window Object,arrowFn(),故指向window。
var name = '全域阿婆'
var auntie = {
name: '漂亮阿姨',
callName: function () { //传统function
console.log('1', this.name); // 1 漂亮阿姨
setTimeout(() => {
console.log('2', this.name); // 2 漂亮阿姨
console.log('3', this); // 3 auntie 这个物件
}, 10);
},
callName2: () => { //arrow function:属於全域变数auntie的物件里面
console.log('4', this.name); // 4 全域阿婆
setTimeout(() => {
console.log('5', this.name); // 5 全域阿婆
console.log('6', this); // 6 window 物件
}, 10);
}
}
var func = function () {
var func2 = function () {
setTimeout(() => {
console.log(this);
}, 10);
};
var func3 = {
func: func2,
var4: 4
}
func2(); // this = window
func3.func(); // func3 Object,因为此时的箭头函式属於物件内属性的值。
}
func();
1.没有arguments
2.this
3.箭头函式无法当作建构函式:无法利用箭头函式来自订原型。
const fn = function (name) {
this.name = name;
};
console.log(fn.prototype); //{constructor: ƒ}
const TOM = new fn('TOM');
console.log(TOM); //fn {name: 'TOM'}
const arrowfn = (name) => {
this.name = name;
}
console.log(arrowfn.prototype); //undefined
const BOB = new arrowfn('BOB');
console.log(BOB); //Uncaught TypeError: arrowfn is not a constructor
const array = [11, 22, 33, 44, 55, 66, 77];
const array02 = array.map(function (params) { return params * 2 });
console.log(array02); //(7) [22, 44, 66, 88, 110, 132, 154]
const array03 = array.map((params) => params * 2);
console.log(array03); //(7) [22, 44, 66, 88, 110, 132, 154]
补充:目标阵列.map
为提取目标阵列内的内容,每笔都做编辑。
先备知识:
Array.from
:提取输入的参数并形成新的阵列.reduce
:会对每一个目前迭代到的阵列元素(除了空值以外)执行 callback 函式,函式内有四个参数(accumulator
、currentValue
、currentIndex(非必要)
、array(非必要)
),因为是抓取阵列的前一个值跟後面一个值做操作,所以会有个一个initialValue
来跟Array[0]
搭配。第一次
被呼叫时,accumulator 与 currentValue 的值可能为两种不同的状况:若在呼叫 reduce()
时有提供 initialValue
,则 accumulator 将会等於 initialValue,且 currentValue 会等於阵列中的第一个元素值;若没有提供 initialValue
,则 accumulator 会等於阵列的第一个元素值,且 currentValue 将会等於阵列的第二个元素值。 const average = function () {
const nums = Array.from(arguments);
const total = nums.reduce(function (acc, val) {
return acc + val
}, 0);
return total / nums.length;
}
console.log(average(1, 2, 3, 4, 5)) // 15(参数和)/5(参数数量)得3
const average02 = (...nums) =>nums.reduce((acc, val) =>acc + val, 0) / nums.length
console.log(average02(1, 2, 3, 4, 5)) //3
参考文章:
前言 这会是jconsole工具的最後一篇介绍了,classes与VM Summary页签的介绍 我...
俗话说人要衣装,佛要金装,我们的 vim 也得要有漂亮的外观。今天就让我们来看看如何调教调整 vim...
当一个需求,经过规画系统的一连串洗礼後,就准备进入开发。对团队而言,工程实践方法,只有最适合,没有最...
Why CSS in JS? 以前在网页开发的时候有一个原则叫做关注点分离(Separation o...
杂谈 今年是个很特别的一年,对威尔猪来说也是,从没写过任何形式的笔记或文章,也没什麽特别规划,居然就...