基本结构:箭头函数左边是参数,右边是返回值
//创建func函数
let func = num => num;
//上面的func函数等价于
let oldFunc = function (num) { return num };
//如果存在多个参数,参数需要括号包起来
let sum = (num1, num2) => num1 + num2;
//上面的sum函数等价于
let oldSum = function (num1, num2) { return num1 + num2; };
//如果返回值需要大量逻辑,并且一个表达式写不完,需要用花括号包起来返回体
let chkUsername = username => { if (username == 'zhang') { return 1; } else if (username == 'li') { return 2; } else { return 3; } }
//箭头函数的返回值支持使用已定义的函数
let getInt = num => parseInt(num);
//箭头函数的this,这是以前this的作用域,在函数中创建闭包,此时的this变了
let userAge = { age: 1, getAge: function () { var fn = function () { return this.age //此时的this指向的是windows } return fn(); }, setAge: function (age) { this.age = age; } }; userAge.setAge(19); console.log(userAge.getAge())
//箭头函数this总是指向词法作用域,也就是外层调用者obj
let userNewAge = { age: 1, getAge: function () { let fn = ()=>(this.age) return fn(); }, setAge: function (age) { this.age = age; } }; userNewAge.setAge(19); console.log(userNewAge.getAge())
window.setInterval(method,time)方法本身会返回一个资源句柄,使用clearInterval(Intervalid)方法即可清除定时器<script> var num=0; //每隔1秒再控制台输...
https://3gimg.qq.com/lightmap/v1/marker/index.html?marker=coord:37.6767600000,112.7297800000&key=TKUBZ-D24AF-GJ4JY-JDVM2-IBYKK-KEBCU&referer=p...
<!doctype html> <html> <head> <meta charset="utf-8"> <title>无标题文档</title> <script type...
<!doctype html> <html> <head> <meta charset="utf-8"> <title>无标题文档</title> </head> ...
代码1:<!--代码开始--> <script src="http://lib.sinaapp.com/js/jquery/1.9.1/jquery-1.9.1.min.js"></script> <script>...
查看文章前你需要了解以下2点://1.this指向的是windows对象,通过console.log(this)可以查看到对象包含所有的方法和属性//2.全局变量属于this对象的属性通过console.log(this)可以查看到对象包含我们设置的全局变量我们经常在创建相同结构的Js对象会重复的设...