码迷,mamicode.com
首页 > 移动开发 > 详细

js中apply方法的使用

时间:2017-08-01 20:49:45      阅读:220      评论:0      收藏:0      [点我收藏+]

标签:name   xtend   property   fine   调用   简洁   apply   hive   this   

1、对象的继承,一般的做法是复制:Object.extend

prototype.js的实现方式是:

Object.extend = function(destination,source){ //destination:目标
     for(property in source){ //property:属性
           destination[property] = source[property];
    }         
    return destination;    
}

 除此之外,还有种方法,就是Function.apply(当然使用Function.call也是可以的)

apply方法能劫持另外一个对象的方法,继承另外一个对象的属性

Function.apply(obj,args) 方法能接收两个参数

  obj:这个对象将代替Function类里的this对象

  args:这个是数组,它将作为参数传给Function(args --> arguments)

apply示范代码如下:

<script>
    function Person(name,age){
        this.name = name;
        this.age = age;
        this.sayhello = function(){
            alert(‘Hello‘)
        }
    }

    function Print(){
        this.funcName = "Print";
        this.show = function(){
            var msg = [];//调用时:["name:tom","age:13","funcName:Print","grade:6","school:清华小学"]
            for (var key in this){ //调用时,this表示:Student,name:tom age:13 grade:6…
                if (typeof (this[key]) != "function") {
                    msg.push([key,":",this[key]].join("")); //join("")把数组中的所有元素放入一个字符串
                }
            }
            console.log(msg.join(" "));
        }
    }

    function Student(name,age,grade,school){
        Person.apply(this,arguments);//this --> Student , arguments --> 0:"tom"  1:13  2:6  3:"清华小学"
        Print.apply(this,arguments);
        this.grade = grade;
        this.school = school;
    }

        var p1 = new Person("jake",10);
        p1.sayhello();

        var s1 = new Student(‘tom‘,13,6,"清华小学");
        s1.show();
        s1.sayhello();
        alert(s1.funcName);
</script>        

  学生类不来不具备任何方法,但是在Person.apply(this,arguments)后,他就具备了Person类的sayhello方法和所有属性。

  在Print.apply(this,arguments)后就自动得到了show()方法

 

2、利用Apply的参数数组化来提高Function.apply()在提高程序性能方面的技巧

  我们先从Math.max()函数说起,Math.max后面可以接任意个参数,最后返回所有参数中的最大值。比如:

  alert(Math.max(5,8)); //8

  alert(Math.max(5,7,9,3,1,6)); //9

  但是在很多情况下,我们需要找出数组中最大的元素。

  var arr = [5,7,9,1];

  alert(Math.max(arr)); //这样却是不行的。一定要这样写

function getMax(arr){
    var arrLen = arr.length;  
    for(var i=0,ret = arr[0]; i<arrLen; i++){
        ret = Math.max(ret,arr[i]);  
    }
    return ret;
}

  这样写麻烦而且低效。如果用apply呢,看代码:

<script>
    function getMax2(arr){
        return Math.max.apply(null,arr); //arr -->  0:5  1:7  2:9  3:1
    }
    var arr = [5,7,9,1];
    console.log(getMax2(arr));
</script>

  两端代码达到了同样的目的,但是getMax2缺优雅,高效,简洁的多。

再比如数组的push方法。

  var arr1 = [1,3,4];

  var arr2 = [3,4,5];

如果我们要把arr2展开,然后一个个追加到arr1中去,最后让arr1 = [1,3,4,3,4,5];

arr1.push(arr2)显然不行的。因为这样做会得到[1,3,4,[3,4,5]]

我们只能用一个循环去一个一个的push(当然也可以用arr1.concat(arr2)),但是concat方法并不改变arr1本身

var arrLen = arr2.length;
for(var i=0; i<arrLen; i++){
    arr1.push(arr2[i]);  
}

  自从有了apply,事情就变得如此简单

Array.prototype.push.apply(arr1,arr2);

 

转自:http://www.cnblogs.com/delin/archive/2010/06/17/1759695.html#undefined

js中apply方法的使用

标签:name   xtend   property   fine   调用   简洁   apply   hive   this   

原文地址:http://www.cnblogs.com/carriezhao/p/7270209.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!