JavaScript进阶函数学习笔记,例如:map、reduce、apply、call,以及this关键字诠释

//在调用函数中修改这个变量,其变量在内部发生改变,返回出来的是新赋予的值。但在外部函数中,其属性值,未发生改变
//也就是说,函数传参的时候,给的是值,不是存储数值的地址,当这个值改变的时候,原来的地址存放的值没有变化,因此不会改变原来的数值
// var color = 'blue';
// function changecolor() {

// if (color === 'blue') {
// color = 'red';
// } else{
// color = 'green';
// }
// return color;
// }

// console.log(color, changecolor(color));//blue red
// //此时打印的color是外部函数的calor值blue,在调用函数changcolor中传入的color是新赋予的red


//this关键字,在一个对象中绑定一个函数,我们叫做这个对象的方法,在这个方法里,存在一个this指针,其指向当前对象,
//只在对象的方法第一层能拿到正确的this,在方法内部重新定义this会指向全局,可以使用that = this捕获第一层的this。然后在方法内部函数使用



// function getAge() {
// var y = new Date().getFullYear();
// return y - this.birth;
// }

// var xiaoming = {
// name: '小明',
// birth: 1990,
// age: getAge
// };

// xiaoming.age(); // 25
// getAge.apply(xiaoming, []); // 25, this指向xiaoming, 参数为空



// function getAge() {
// var y = new Date().getFullYear();
// return y - this.birth;
// }

// var xiaoming = {
// name: '小明',
// birth: 1990,
// age: getAge
// };

// xiaoming.age(); // 25, 正常结果
// getAge(); // NaN


// 另一个与apply()类似的方法是call(),唯一区别是:

// apply()把参数打包成Array再传入;
//apply('指向的对象',[需要传递的参数]);

// call()把参数按顺序传入。

// 比如调用Math.max(3, 5, 4),分别用apply()和call()实现如下:

// Math.max.apply(null, [3, 5, 4]); // 5
// Math.max.call(null, 3, 5, 4); // 5
// 对普通函数调用,我们通常把this绑定为null。
// 计算函数被调用几次

// 'use strict'
// var count = 0;
// var oldParseInt = parseInt;//保留原函数的功能
// window.parseInt = function () {
// count += 1; //保留函数功能的情况下,增加一个函数被调用的功能
// return oldParseInt.apply(null,arguments);
// }

// parseInt('1');
// parseInt('2');
// parseInt('3');
// console.log('count= ' + count);
// console.log(parseInt('0x10'));
//apply装饰器的原理,不改变原函数的情况下,给原函数增加新功能,在调用

//利用apply()可以动态指向想要指向的对象,以及想传递的参数
 
//高阶函数 一个函数可以接收另一个函数作为参数,这种函数成为高阶函数
// function add(x,y,f) {
// return f(x) + f(y);
// }

// var x=add(-2, 4, Math.abs);
// console.log(x);



//数组的map方法,将函数作用在数组里,将数组里的每一个元素逐步传入函数,并获得新的一组数组结果 其接受一个参数
//数组多长,就调用几次函数,例如:长度为2的数组:[1,2],则调用函数时。将调用两次,例如:arr.map(function add(x){return x*x}) 得到新数组结果为 [1,4]
// function pow(x) {
// return x * x;
// }

// var arr1 = [1, 2, 3, 4, 5, 6];
// var results = arr1.map(pow);//一次放入数组元素,并求得平方
// console.log(results);//[1, 4, 9, 16, 25, 36]



//数组reduce方法 将函数作用在数组里,将数组里的每一个元素,分成前后传入函数,并获得一组数组结果,其接受两个参数
//数组多长,就调用除以2次函数,例如:长度长度为4;[1,2,3,4],则调用函数时,将函数调用2次。例如:arr.reduce(function sub(x,y){return x-y}) 得到的新数组结果为[-1,-1]

// var arr = [1, 2, 3, 4];
// arr.reduce(function (x, y) {
// return res= x * y;
// });

// console.log(res);

// 'use strict';


// console.log(arr);
// arr.reduce(
// (x,y)=>{
// res= x * y;
// }
// );
// }



// 使用reduce求任意数组的乘积
// function product(arr) {
// return arr.reduce(function (x, y) {
// return x*y
// });
 
// }
// // 测试:
// if (product([99, 88, 77, 66]) === 44274384) {
// console.log('测试通过!');
// }
// else {
// console.log('测试失败!');
// }

// //使用reduce将任意数组中的所有元素合并成一个整数
// var arr = [1, 3, 5, 7, 9];
// var num = arr.reduce(function (x, y) {
// console.log(x, y);
// console.log('x='+x * 10 +'+'+ y+'y='+y);
// return x * 10 + y;
 
// },);

// console.log(num);

// 把字符串转换成整数
// 思路:现将字符串转换成数组,然后将数组转换成整数
// function string2int(s) {
 


// var arr = s.split('');
// console.log(arr);
// var arr2 = eval('[' + String(arr) + ']')
// var num = arr2.reduce(function (x, y) {
// console.log(x,y);
// return x * 10 + y;
// });
// return num
 
// }


// // 测试:
// if (string2int('0') === 0 && string2int('12345') === 12345 && string2int('12300') === 12300) {
// if (string2int.toString().indexOf('parseInt') !== -1) {
// console.log('请勿使用parseInt()!');
// } else if (string2int.toString().indexOf('Number') !== -1) {
// console.log('请勿使用Number()!');
// } else {
// console.log('测试通过!');
// }
// }
// else {
// console.log('测试失败!');
// }
// 将用户输入的英文名,首字母大写,其他小写
//思路:将用户输入的英文名首字母做大写处理,再将其与字母做小写处理,最后拼接在一起
// 'use strict'
// function normalize(arr) {
// var userNmae = arr.map(function (name) {
// return name.toString()[0].toUpperCase() + name.substring(1).toLowerCase();
// });
// return userNmae
// }


// //测试:
// if (normalize(['adam', 'LISA', 'barT']).toString() === ['Adam', 'Lisa', 'Bart'].toString()) {
// console.log('测试通过!');
// }
// else {
// console.log('测试失败!');
// }


//利用map把字符串变为整数

// 'use strict';

// var arr = ['1', '2', '3'];
// var r;

// r = arr.map(Number);


// console.log(r);







posted @ 2022-04-01 10:19  Chiffon1996  阅读(51)  评论(0)    收藏  举报