
一.函数表达式的语法形式
- 匿名函数
var functionName = function(arg0, arg1, arg2) {
//函数体
}
二.函数表达式没有函数提升
var a = 1;
if(a != 1) {
function sayHi() {
console.log("Hi");
}
} else {
function sayHi() {
console.log("Hello");
}
}
sayHi(); //sayHi() is not defined
var say;
if (a == 1) {
say = function() {
console.log("Hi");
}
} else {
say = function() {
console.log("Hello");
}
}
say(); //Hi
二.使用函数实现递归
function f (num) {
if (num <= 1) {
return 1;
} else {
return num * f(num-1);
}
} var otherF = f;
f = null;
console.log(otherF(1)); //
console.log(otherF(4)); //error : f is not a function
function f2(num) {
if (num <= 1) {
return 1;
} else {
return num * arguments.callee(num-1);
}
} var otherF = f2;
f2 = null;
console.log(otherF(1)); //
console.log(otherF(3)); // 6,严格模式下会出错
var f3 = (function ff(num) {
if (num <= 1) {
return 1;
} else {
return num * ff(num - 1);
}
}); //将函数表达式赋值给变量
console.log(f3(1)); //
console.log(f3(3)); // 6,严格模式和非严格模式都可以
三.闭包
- 闭包中外部函数在执行完毕后,其活动对象也不会被销毁,因为闭包中返回的内部函数的作用域链仍然在引用这个活动对象。直到匿名函数被销毁后,外部函数的活动对象才会被销毁。
- 作用域链的这种配置机制引出了一个值得注意的副作用,即闭包只能取得包含函数中任何变量的最后一个值。
function createFunction() {
var result = new Array(); for (var i = 0; i < 10; i++) {
result[i] = function() {
return i;
};
} return result;
}
var re = createFunction();
/*
re是一个函数数组,表面上看每个函数都要返回自己的索引值。
但实际上,每个函数都返回10.因为每个函数的作用域中对保存
着createFunction()函数的活动对象,所以它们引用的都是同一个变量i
*/
console.log(re[1]()); //function createFunction() {
var result = new Array(); for (var i = 0; i < 10; i++) {
result[i] = function(num) {
return function() {
return num;
}//函数参数按值传递
}(i);
} return result;
}
var re = createFunction();
console.log(re[1]()); //
四.模仿块级作用域
这种技术通常在全局作用域中被用在函数外部,从而限制向全局作用域中添加过多的变量和函数。
而通过创建私有作用域,每个开发人员即可以使用自己的变量,又不必担心搞乱全局作用域。
(function () {
var now = new Date();
console.log(now.getMonth());
})();
五.私有变量
1.构造函数模式
function Person(name) {
this.getName = function() {
return name;
}; this.setName = function(value) {
name = value;
}
} var pserson = new Person("A");
console.log(pserson.getName()); //A
pserson.setName("B");
console.log(pserson.getName()); //B
【有权访问私有变量和私有函数的公有方法称为特权方法。能够在构造函数中定义特权方法,是因为特权方法作为闭包有权访问在构造函数中定义的所有变量和函数。
利用私有和特权成员,可以隐藏那些不应该被直接修改的数据。构造函数模式的缺点是针对每个实例都会创建同样一组新方法。】
2.通过私有作用域
(function() {
var name = "";
Person = function(value) {
name = value;
}; Person.prototype.getName = function() {
return name;
} Person.prototype.setName = function(value) {
name = value;
}
})(); var p1 = new Person("1");
console.log(p1.getName()); //
p1.setName("2");
console.log(p1.getName()); // var p2 = new Person("3");
console.log(p1.getName()); //
console.log(p2.getName()); //
【上面的例子中,变量name就变成了一个静态的,由所有实例共享的属性。以这种方式创建静态私有变量会因为使用原型而增加代码复用,但每个实例都没有自己的私有变量。】
六.单例模式
var application = function() {
//私有变量和函数
var components = new Array(); //初始化
components.push(new BaseComponent()); //公共
return {
getComponentCount : function() {
return components.length;
}, registerComponent : function(component) {
if (typeof component == "object") {
components.push(component);
}
}
};
}();
七. 增强的单例模式
【适合那些单例必须是某些类型的实例,同时还必须增加某些属性,方法对其加以增强的情况】
var application = function() {
//私有变量和函数
var components = new Array(); //初始化
components.push(new BaseComponent()); //创建application的一个局部副本
var app = new BaseComponent(); //公共接口
app.getComponentCount = function() {
return components.length;
}; app.registerComponent = function(component) {
if (typeof component == "object") {
components.push(component);
}
}; return app;
}();