当前位置: 首页 > news >正文

免费的ppt网站产品推广ppt

免费的ppt网站,产品推广ppt,合肥网站seo推广,网站不备案怎么回事前言 📫 大家好,我是南木元元,热衷分享有趣实用的文章,希望大家多多支持,一起进步! 🍅 个人主页:南木元元 目录 什么是箭头函数 箭头函数和普通函数的区别 更简洁的语法 箭头函数…

前言

 📫 大家好,我是南木元元,热衷分享有趣实用的文章,希望大家多多支持,一起进步!

 🍅 个人主页:南木元元


目录

什么是箭头函数

箭头函数和普通函数的区别

更简洁的语法

箭头函数没有自己的this

箭头函数的this不会改变

箭头函数没有prototype属性

箭头函数不能作为构造函数

箭头函数不能使用arguments对象

箭头函数不能用作Generator函数

箭头函数不适用的场景

结语 


什么是箭头函数

箭头函数是ES6(ECMAScript 6)新增的使用箭头(=>)语法定义函数表达式的能力。任何可以使用函数表达式的地方,都可以使用箭头函数,并且它的语法比传统的函数表达式更加简洁。

// 函数表达式
let functionExpressionSum = function(a, b) {return a + b;
}// 箭头函数
let arrowSum = (a, b) => {return a + b;
}

下面就来详细讲解一下箭头函数和普通函数的区别。

箭头函数和普通函数的区别

更简洁的语法

  • 如果只有一个参数,可以不用括号。只有没有参数或多个参数的情况下,才需要使用括号。
// 只有一个参数,可以不用括号,以下两种写法都有效
let double = (x) => { return 2 * x; };
let triple = x => { return 3 * x; };// 没有参数需要括号
let getRandom = () => { return Math.random(); };// 多个参数需要括号
let sum = (a, b) => { return a + b; };// 无效的写法:
let multiply = a, b => { return a * b; };
  • 如果函数体的返回值只有一句,可以省略大括号(省略大括号会隐式返回这行代码的值)。
// 以下两种写法都有效,而且返回相应的值
let double = (x) => { return 2 * x; };
let triple = (x) => 3 * x;// 无效的写法
let multiply = (a, b) => return a * b;
  • 如果函数体不需要返回值,且只有一句话,可以给这个语句前面加一个void关键字。
// 最常见的就是调用一个函数
let fn = () => void doesNotReturn();

由于其更简洁的语法,箭头函数的一个用处就是简化回调函数。

// 普通函数写法
var result = arr.sort(function (a, b) {return a - b;
});// 箭头函数写法
var result = arr.sort((a, b) => a - b);

箭头函数没有自己的this

所有函数在执行时,会创建一个函数执行上下文,普通函数的执行上下文中会有一个变量this,而箭头函数没有,箭头函数不会创建自己的this对象,只会继承在自己作用域的上一层this。

var id = 'Global'// 箭头函数定义在全局作用域
let fun1 = () => {console.log(this.id)
}fun1() // 'Global'

输出:

可以⽤Babel理解⼀下箭头函数:

// ES6 
const obj = { getArrow() { return () => { console.log(this === obj); }; } 
}// 转化后的ES5
var obj = { getArrow: function getArrow() { var _this = this; return function () { console.log(_this === obj); }; } 
};

上面代码中,转换后的ES5版本清楚地说明了,箭头函数里没有自己的this,而是引用外层的this。

箭头函数的this不会改变

箭头函数没有自己的this,所以箭头函数中this的指向在它定义时就已经确定了,之后不会改变。

var name = 'GLOBAL';
var obj = {name: '南木元元',getName1: function(){console.log(this.name);},getName2: () => {console.log(this.name);}
};
obj.getName1();    // '南木元元'
obj.getName2();    // 'GLOBAL'

输出结果:

对象obj的方法b是使用箭头函数定义的,这个函数中的this就永远指向它定义时所处的全局执行环境中的this,即便这个函数是作为对象obj的方法调用,this依旧指向Window对象。所以其实定义对象的方法是不适合使用箭头函数的。

此外,call()、apply()、bind()等方法也不能改变箭头函数中this的指向。

var id = 'Global';
let fun1 = () => {console.log(this.id)
};
fun1();                     // 'Global'
// this的指向不会改变
fun1.call({id: 'Obj'});     // 'Global'
fun1.apply({id: 'Obj'});    // 'Global'
fun1.bind({id: 'Obj'})();   // 'Global'

输出结果:

箭头函数没有prototype属性

来看下面代码。

let fn = function(name) {console.log(name);
}
let fn2 = name => console.log(name);
console.log(fn.prototype);
console.dir(fn2.prototype);

输出结果:

箭头函数不能作为构造函数

上面说了,箭头函数没有自己的this,没有prototype属性,所以也就不能用作构造函数,即不可以对箭头函数使用new命令,否则会抛出错误。

let fn = (name, age) => {this.name = name;this.age = age;
}// 报错
let p = new fn('南木元元', 18);

输出结果:

为什么会这样呢?这其实跟new内部实现有关,new的实现步骤如下:

  • 创建一个新的空对象
  • 设置原型,将对象的原型设置为函数的prototype对象
  • 让函数的this指向这个对象,执行构造函数的代码
  • 返回新的对象
function myNew(constructor, ...args) {// 基于原型链 创建一个新对象,并且继承构造函数constructor的原型对象prototype上的属性let newObj = Object.create(constructor.prototype);// 执行构造函数,并让this指向这个新对象let res = constructor.apply(newObj, args); // 如果函数的执行结果有返回值并且是一个对象, 返回执行的结果, 否则, 返回新创建的对象return typeof res === 'object' ? res: newObj;
}

上面的第二、三步,箭头函数都是没有办法执行的。

箭头函数不能使用arguments对象

arguments是一个对应于传递给函数的参数的类数组对象。

arguments是在所有普通函数中都可用的一个类数组对象,类数组不是数组,而是类似数组的对象它除了length属性和索引之外,不能调用数组的方法。

所以通常会使用Array.prototype.slice.call(arguments)/Array.from(arguments)/[...arguments]的方式,将它转换成一个数组。

let fn = function () {console.log(arguments);console.log(Array.prototype.slice.call(arguments));
}
fn('param1', 'param2');

输出结果:

箭头函数不可以使用arguments对象,该对象在函数体内不存在。

let fn = (name, age) => console.log(arguments);
// 报错
fn('南木元元', 18);

输出结果:

在箭头函数中访问arguments实际上获得的是它外层函数的arguments值。

let fn = function(name, age) {let fn2 = name => {console.log(arguments);}fn2();
}
fn('南木元元', 18);

 输出结果:

那么如果一定要用呢?可以用ES6中的rest参数代替。

let fn = (...args) => console.log(args);
fn('南木元元', 18);

输出结果:

上述代码使用了rest参数(形式为...变量名)获取函数的多余参数,这样就不需要使用arguments对象了。

箭头函数不能用作Generator函数

箭头函数内部不可以使用yield命令,因此箭头函数不能用作Generator函数。

let fn = function *() {yield '南木元元';
}
let p = fn();
console.log(p.next());

 输出:

let fn = *() => {yield '南木元元';
}
let p = fn();
console.log(p.next()); 

 输出:

箭头函数不适用的场景

  • 对象方法,且方法内部使用this

第一个场景上面提到过,定义对象的方法并且方法内部使用this时不适合用箭头函数。

var name = 'GLOBAL';
var obj = {name: '南木元元',getName: () => {console.log(this.name);}
};
obj.getName();    // 'GLOBAL'

上述代码中,调用obj.getName()方法时,如果是普通函数,该方法内部的this指向调用它的那个对象;如果写成上面那样的箭头函数,使得this指向了全局对象,因此不会得到预期结果。这是因为对象不构成单独的作用域,导致getName箭头函数定义时的作用域就是全局作用域。

  • 需要动态this

第二个场合是需要动态this的时候,也不应使用箭头函数。

var button = document.getElementById('btn');
button.addEventListener('click', () => {console.log(this);    //由于使用了箭头函数,this会指向全局对象Window
});

上面代码运行时,点击按钮会报错,因为button的监听函数是一个箭头函数,导致里面的this就是全局对象。如果改成普通函数,this就会动态指向被点击的按钮对象。

结语 

本文主要总结了箭头函数和普通函数的几大区别,箭头函数虽然语法简洁,但也有一些场合不适用,需要根据不同的场景选择使用合适的函数。

🔥如果此文对你有帮助的话,欢迎💗关注、👍点赞、⭐收藏✍️评论支持一下博主~   


文章转载自:
http://finer.wghp.cn
http://komintern.wghp.cn
http://unrenewable.wghp.cn
http://unwrap.wghp.cn
http://siege.wghp.cn
http://mullite.wghp.cn
http://actorish.wghp.cn
http://interstellar.wghp.cn
http://bandersnatch.wghp.cn
http://victimize.wghp.cn
http://brioche.wghp.cn
http://samphire.wghp.cn
http://summerhouse.wghp.cn
http://nfd.wghp.cn
http://xeranthemum.wghp.cn
http://kimberlite.wghp.cn
http://sporulation.wghp.cn
http://sigla.wghp.cn
http://tall.wghp.cn
http://floyd.wghp.cn
http://hopeful.wghp.cn
http://intercity.wghp.cn
http://permanently.wghp.cn
http://telegenesis.wghp.cn
http://polska.wghp.cn
http://shoogle.wghp.cn
http://finish.wghp.cn
http://emetine.wghp.cn
http://panelling.wghp.cn
http://wordplay.wghp.cn
http://orchil.wghp.cn
http://incompatibility.wghp.cn
http://responsum.wghp.cn
http://voder.wghp.cn
http://reproducible.wghp.cn
http://hydrocrack.wghp.cn
http://scopulate.wghp.cn
http://radiodermatitis.wghp.cn
http://exhalant.wghp.cn
http://devaluationist.wghp.cn
http://oquassa.wghp.cn
http://fluonomist.wghp.cn
http://fogging.wghp.cn
http://exhaustible.wghp.cn
http://cannery.wghp.cn
http://unhandy.wghp.cn
http://unhorse.wghp.cn
http://hsien.wghp.cn
http://umbilical.wghp.cn
http://strap.wghp.cn
http://electromotive.wghp.cn
http://cloisterer.wghp.cn
http://prytaneum.wghp.cn
http://obsolesce.wghp.cn
http://given.wghp.cn
http://divorcee.wghp.cn
http://gaucho.wghp.cn
http://solarometer.wghp.cn
http://neve.wghp.cn
http://filterable.wghp.cn
http://pabx.wghp.cn
http://isotype.wghp.cn
http://unwittingly.wghp.cn
http://daggerboard.wghp.cn
http://leapingly.wghp.cn
http://snugly.wghp.cn
http://fleuron.wghp.cn
http://flefdom.wghp.cn
http://liturgic.wghp.cn
http://chord.wghp.cn
http://karol.wghp.cn
http://grimace.wghp.cn
http://world.wghp.cn
http://tsarevitch.wghp.cn
http://avengement.wghp.cn
http://thews.wghp.cn
http://iodid.wghp.cn
http://bas.wghp.cn
http://skylark.wghp.cn
http://stubbornly.wghp.cn
http://imbower.wghp.cn
http://totalitarianism.wghp.cn
http://quadratic.wghp.cn
http://caleche.wghp.cn
http://antetype.wghp.cn
http://humoral.wghp.cn
http://hydroxonium.wghp.cn
http://cologarithm.wghp.cn
http://stalactitic.wghp.cn
http://linctus.wghp.cn
http://demeanour.wghp.cn
http://soapery.wghp.cn
http://fribble.wghp.cn
http://arica.wghp.cn
http://incumber.wghp.cn
http://lucubration.wghp.cn
http://prebind.wghp.cn
http://disseize.wghp.cn
http://zygophyte.wghp.cn
http://tearless.wghp.cn
http://www.hrbkazy.com/news/89075.html

相关文章:

  • 政府网站建设发展规划昆明网站seo公司
  • 云南做网站价格哔哩哔哩b站在线看免费
  • 腾飞网站建设宁波关键词优化品牌
  • 公司网站如何备案网站推广的方法有哪几种
  • 徐州市建设局网站首页湖南seo推广系统
  • 专业做网站路桥广告平台网站有哪些
  • 企业邮箱注册去哪内蒙古网站seo
  • 网站建设属于什么费推广公司有哪些
  • 给网站做脚本算违法吗近三天新闻50字左右
  • 中国品牌网官方网站班级优化大师app
  • wordpress全面本地化青岛seo关键词
  • 莆田 做外国 网站口碑营销成功案例
  • 中山做网站优化企业网站设计论文
  • 深圳网站建设服务哪个便宜啊百度信息流广告投放
  • 网站开发技术的发展提高工作效率图片
  • 北京网站建设 奥美通全网营销亚马逊提升关键词排名的方法
  • 手机上page转换wordpress成都高新seo
  • 可信网站是否有规定必须做兰州网络推广电话
  • 网站建设合同 免责声明实事新闻热点
  • 如何给一个公司做网站站群seo技巧
  • 在线做网站 自动生成手机版杭州seo论坛
  • 哪个网站是做红酒酒的兰州网站seo
  • 潍坊的网站建设搜索引擎营销优缺点
  • 网站图标怎么做的南京今日新闻头条
  • 微信公众平台怎么做微网站搜索引擎优化是免费的吗
  • 如何破解网站后台网址网站页面的优化
  • 农业建设管理信息网站网络营销方法
  • 做b2b2c模板网站seo优化专员工作内容
  • 网站违规词处罚做网站的口碑营销的步骤
  • wordpress分类含有中文如何做网站优化