js 函数
////// 函数
const x = (x, y) => x * y; // 如果只有一句,可以省略return和大括号,最好加上
// 箭头函数没有 this,但是作为对象的属性可以有
//函数参数(parameter)指的是在函数定义中列出的名称。
//函数参数(argument)指的是传递到函数或由函数接收到的真实值。
function findMax() {
console.log(arguments) //获取全部参数
}
- //// call 方法
//使用 call() 方法,您可以编写能够在不同对象上使用的方法。它可以用来调用所有者对象作为参数的方法
var person = {
fullName: function () {
return this.firstName + " " + this.lastName;
}
}
var person1 = {
firstName: "Bill",
lastName: "Gates",
}
person.fullName.call(person1);
// 方法要是有参数,call的对象参数放在第一个
person.fullName.call(person1, "Seattle", "USA");
- //// apply 方法
//通过 apply() 方法,您能够编写用于不同对象的方法。
person.fullName.apply(person1);
//和call 区别在于,apply接收的参数为数组
person.fullName.apply(person1, ["Oslo", "Norway"]);
person.fullName.call(person1, "Oslo", "Norway");
- //// 闭包,处理局部变量和全局变量的问题
var add = (function () {
var counter = 0;
return function () {return counter += 1;}
})();
add();
add();
add();

浙公网安备 33010602011771号