JavaScript难点(二)

来源:互联网 发布:vb.net chart控件使用 编辑:程序博客网 时间:2024/05/16 08:10

6. 变量提升

JavaScript会将所有变量和函数声明移动到它的作用域的最前面,这就是所谓的变量提升(Hoisting)。也就是说,无论你在什么地方声明变量和函数,解释器都会将它们移动到作用域的最前面。因此我们可以先使用变量和函数,而后声明它们。

但是,仅仅是变量声明被提升了,而变量赋值不会被提升。如果你不明白这一点,有时则会出错:

console.log(y);  // 输出undefinedy = 2; // 初始化y

上面的代码等价于下面的代码:

var y;  // 声明yconsole.log(y);  // 输出undefinedy = 2; // 初始化y

为了避免BUG,开发者应该在每个作用域开始时声明变量和函数。

7. 柯里化

柯里化,即Currying,可以是函数变得更加灵活。我们可以一次性传入多个参数调用它;也可以只传入一部分参数来调用它,让它返回一个函数去处理剩下的参数。

var add = function(x) {    return function(y) {        return x + y;    };};console.log(add(1)(1)); // 输出2var add1 = add(1);console.log(add1(1)); // 输出2var add10 = add(10);console.log(add10(1)); // 输出11

代码中,我们可以一次性传入2个1作为参数add(1)(1),也可以传入1个参数之后获取add1add10函数,这样使用起来非常灵活。

8. apply, call与bind方法

JavaScript开发者有必要理解applycallbind方法的不同点。它们的共同点是第一个参数都是this,即函数运行时依赖的上下文。

三者之中,call方法是最简单的,它等价于指定this值调用函数:

var user = {    name: "Rahul Mhatre",    whatIsYourName: function() {        console.log(this.name);    }};user.whatIsYourName(); // 输出"Rahul Mhatre",var user2 = {    name: "Neha Sampat"};user.whatIsYourName.call(user2); // 输出"Neha Sampat"

apply方法与call方法类似。两者唯一的不同点在于,apply方法使用数组指定参数,而call方法每个参数单独需要指定:

  • apply(thisArg, [argsArray])
  • call(thisArg, arg1, arg2, …)
var user = {    greet: "Hello!",    greetUser: function(userName) {        console.log(this.greet + " " + userName);    }};var greet1 = {    greet: "Hola"};user.greetUser.call(greet1, "Rahul"); // 输出"Hola Rahul"user.greetUser.apply(greet1, ["Rahul"]); // 输出"Hola Rahul"

使用bind方法,可以为函数绑定this值,然后作为一个新的函数返回:

var user = {     greet: "Hello!",     greetUser: function(userName) {     console.log(this.greet + " " + userName);     }};var greetHola = user.greetUser.bind({greet: "Hola"});var greetBonjour = user.greetUser.bind({greet: "Bonjour"});greetHola("Rahul") // 输出"Hola Rahul"greetBonjour("Rahul") // 输出"Bonjour Rahul"

9. Memoization

Memoization用于优化比较耗时的计算,通过将计算结果缓存到内存中,这样对于同样的输入值,下次只需要中内存中读取结果。

function memoizeFunction(func){    var cache = {};    return function()    {        var key = arguments[0];        if (cache[key])        {            return cache[key];        }        else        {            var val = func.apply(this, arguments);            cache[key] = val;            return val;        }    };}var fibonacci = memoizeFunction(function(n){    return (n === 0 || n === 1) ? n : fibonacci(n - 1) + fibonacci(n - 2);});console.log(fibonacci(100)); // 输出354224848179262000000console.log(fibonacci(100)); // 输出354224848179262000000

代码中,第2次计算fibonacci(100)则只需要在内存中直接读取结果。

10. 函数重载

所谓函数重载(method overloading),就是函数名称一样,但是输入输出不一样。或者说,允许某个函数有各种不同输入,根据不同的输入,返回不同的结果。凭直觉,函数重载可以通过if...else或者switch实现,这就不去管它了。jQuery之父John Resig提出了一个非常巧(bian)妙(tai)的方法,利用了闭包。

从效果上来说,people对象的find方法允许3种不同的输入: 0个参数时,返回所有人名;1个参数时,根据firstName查找人名并返回;2个参数时,根据完整的名称查找人名并返回。

难点在于,people.find只能绑定一个函数,那它为何可以处理3种不同的输入呢?它不可能同时绑定3个函数find0,find1find2啊!这里的关键在于old属性。

addMethod函数的调用顺序可知,people.find最终绑定的是find2函数。然而,在绑定find2时,oldfind1;同理,绑定find1时,oldfind0。3个函数find0,find1find2就这样通过闭包链接起来了。

根据addMethod的逻辑,当f.lengtharguments.length不匹配时,就会去调用old,直到匹配为止。

function addMethod(object, name, f){      var old = object[name];      object[name] = function()    {        // f.length为函数定义时的参数个数        // arguments.length为函数调用时的参数个数            if (f.length === arguments.length)        {              return f.apply(this, arguments);            }        else if (typeof old === "function")        {            return old.apply(this, arguments);            }      };}// 不传参数时,返回所有namefunction find0(){      return this.names;}// 传一个参数时,返回firstName匹配的namefunction find1(firstName){      var result = [];      for (var i = 0; i < this.names.length; i++)    {            if (this.names[i].indexOf(firstName) === 0)        {                  result.push(this.names[i]);            }      }      return result;}// 传两个参数时,返回firstName和lastName都匹配的namefunction find2(firstName, lastName){     var result = [];      for (var i = 0; i < this.names.length; i++)    {            if (this.names[i] === (firstName + " " + lastName))        {                  result.push(this.names[i]);            }      }      return result;}var people = {      names: ["Dean Edwards", "Alex Russell", "Dean Tom"]};addMethod(people, "find", find0);addMethod(people, "find", find1);addMethod(people, "find", find2);console.log(people.find()); // 输出["Dean Edwards", "Alex Russell", "Dean Tom"]console.log(people.find("Dean")); // 输出["Dean Edwards", "Dean Tom"]console.log(people.find("Dean", "Edwards")); // 输出["Dean Edwards"]