/* 普通函数 / // function fn(str){ // let msg=str+'hello' // return msg; // } // let a=fn('张三丰'); // console.log(a); / 普通函数可以做为构造函数 */ // let f=new fn(); // console.log(f);
/* 箭头函数 */
/* 箭头=>后面只有一段表达式代表返回return这段表达式 */
// let fn= (str) =>str+'hello';
// let a=fn('张无忌');
// console.log(a);
/* 像这样后面如果有多段表达式 需要使用{}包裹,并且手动return。这种目前打印出来的和上一个一样 */
/* 箭头函数如果只有一个参数()可以省略不写
多个参数的话是需要加()的 */
// let fn=(str1,str2)=>{
// let msg=str + str2
// return msg
// };
// let a = fn('张无忌','起飞');
// document.write(a)
/* 箭头函数是不可以做为构造函数,会报错 */
/* 普通函数的this是谁调用就是谁的,都没有调用默认window
箭头函数没有自己的tihs 箭头函数的this是上下文环境的this */
// let obj1={
// name:'aaa',
// fn:function(){
// console.log(this);
// }
// }
// obj1.fn();/* 这里的this就是obj1的函数 */
// let obj2={
// name:'bbb'
// }
// obj1.fn.call(obj2)/* 这里的this就是bbb,普通的函数可以被call或者apply修改 */
/* 如果一个参数都没有,大括号不可以省略 */
// let obj1={
// name:'aaa',
// fn:()=>{
// /* 目前的环境下的this是window */
// console.log(this);
// }
// }
// obj1.fn()/* 这样调用的this还是window */
// obj1.fn();
// let obj2={
// name:'bbb'
// }
// obj1.fn.call(obj2)/* 这样还是window,是改不了的。箭头函数的this不会被call或者apply修改 */