在学习JS继承的时候,有一段代码:
var initializing = false;function Person() {if (!initializing) {this.init.apply(this, arguments); }}Person.prototype = {init: function(name) {this.name = name;},getName: function() {return this.name;}}
请教各位大侠,这一行“this.init.apply(this, arguments); ”是什么意思?

解决方案 »

  1.   

    参考
    http://www.popo4j.com/article/the-differences-of-apply-and-call.html
      

  2.   

    this.init.apply(this, arguments); 
    其实是这个意思
    ----------
    初始化一个对象  new Person("x","y","z")
    arguments类似与数组,那里面就有三个值 x ,y ,z
    p1 = argumens[0] //值是 x
    p2 = argumens[1] //y
    p3 = argumens[2] //z
    把那句你看不懂的代码其实就可以这样写了
    this.init(p1,p2,p3)
      

  3.   

    就是一个方法调用,参数:
    this:这个不用解释了吧,你在哪里调用的,this就是那里的所属对象
    arguments:你在哪个方法下面调用了this.init.apply方法,这个arguments就是该方法的所有参数
    例如
    function test(p1,p2,p3){
       this.init.apply(this,arguments);
    }
    这个时候,arguments就等于[p1,p2,p3],可以通过数组的形式来访问arguments[0],arguments[1],arguments[2]明白不?
      

  4.   

    arguments  就是指函数的参数
    apply方法这里就是把上面的参数应用到init()函数里调用....