立即执行函数
- 代码
(function () {
})();
- 创建函数的同时立即执行,没有绑定任何事件,也无需等待任何异步操作
function () {}
是一个匿名函数,包围它的一对括号将其转换为一个表达式,紧跟其后的一对括号调用了这个函数。立即执行函数也可以理解为立即调用一个匿名函数。最常见的应用场景就是:将变量的作用于限制于函数内,避免命名冲突。- 实现代码模块化。
闭包
使用闭包定义私有变量
function P() {
let name;
this.setName = function(value) {
name = value;
}
this.getName = function() {
return name;
}
}
let p = new P();
p.setName("Func");
console.log(p.name);
console.log(p.getName());
prototype
- 每个 JavaScript 的构造函数都有一个
prototype
属性,用于设置所有实例对象需要共享的属性和方法。 prototype
属性不能列举。- JavaScript 仅支持通过
prototype
属性进行继承属性和方法。function Func(x, y) {
this.length = x;
this.width = y;
}
Func.prototype.getDimensions = function() {
return {
length: this.length,
width: this.width
}
}
let x = new Func(3, 4);
let y = new Func(5, 6);
console.log(x.getDimensions());
console.log(y.getDimensions());
变量提升
- 函数首先被提升,然后才是变量。
- 函数提升优先级比变量提升要高,且不会被变量声明覆盖,但是会被变量赋值覆盖。
- 当变量(仅声明未初始化或赋值)和函数同名时,引用变量名(函数名)的位置如果是在变量被赋值之前,那么此时引用的就是函数;如果引用是在变量赋值之后,那么引用的就是变量。
- 函数声明和变量声明提升以及优先级
柯里化
函数重载
- 理解下面代码需要知道一个知识点
let len = (function() {}).length;
console.log(len);
let len1 = (function(a) {}).length;
console.log(len1);
let len2 = (function(a, b) {}).length;
console.log(len2);
- 一个
function
直接.length
返回的是函数期望传入的参数数量,即形参的个数。
-
function addMethod(object, name, f) {
let old = object[name];
object[name] = function() {
if (f.length == arguments.length) {
return f.apply(this, arguments);
} else if (typeof old == "function") {
return old.apply(this, arguments);
}
}
}
function find0() {
return this.names;
}
function find1(firstName) {
let result = [];
for (let i = 0; i < this.names.length; i++) {
if (this.names[i].indexOf(firstName) == 0) result.push(this.names[i]);
}
return result;
}
function find2(firstName, secondName) {
let result = [];
for (let i = 0; i < this.names.length; i++) {
if (this.names[i] == firstName + " " + secondName) result.push(this.names[i]);
}
return result;
}
let people = {names: ["Tom Jerry", "Tom Dog", "Jerry Dog"]};
addMethod(people, "find", find0);
addMethod(people, "find", find1);
addMethod(people, "find", find2);
console.log(people.find());
console.log(people.find("Tom"));
console.log(people.find("Tom", "Jerry"));
- 从效果上来说,
people
对象的find
方法允许 3 种不同的输入:0 个参数时,返回所有人名;1 个参数时,根据 firstname 查找人名并返回;2 个参数时,根据完整的名称查找人名并返回。 - 难点在于,
people.find
只能绑定一个函数,那它为何可以处理3种不同的输入呢? - 就此片段中
addMethod
函数的调用顺序可知,people.find
最终绑定的是find2
函数,然而在绑定find2
时,old
为find1
;绑定find1
时,old
为find0
。3 个函数通过闭包链接起来。