Please, can someone tell me what does this.init.apply(this, arguments)
do in the code below?
I understand what apply()
does in general, but in the context of the code below, what is it doing in there?
var Class = function() {
var klass = function() {
this.init.apply(this, arguments); //I don't really get this bit...
};
klass.prototype.init = function(){};
return klass;
};
var Person = new Class;
//Usage
var someone = new Person;
I see a lot of people using it. I've got an idea of what it does but can't really put my hands on it so I need more light.
I'm going up an extra level in JS, so I wanna know everything about it, not just the simple 'Hello world' level.
Many thanks
Please, can someone tell me what does this.init.apply(this, arguments)
do in the code below?
I understand what apply()
does in general, but in the context of the code below, what is it doing in there?
var Class = function() {
var klass = function() {
this.init.apply(this, arguments); //I don't really get this bit...
};
klass.prototype.init = function(){};
return klass;
};
var Person = new Class;
//Usage
var someone = new Person;
I see a lot of people using it. I've got an idea of what it does but can't really put my hands on it so I need more light.
I'm going up an extra level in JS, so I wanna know everything about it, not just the simple 'Hello world' level.
Many thanks
Share Improve this question edited May 15, 2012 at 19:54 Shaoz asked May 15, 2012 at 19:47 ShaozShaoz 10.7k26 gold badges75 silver badges100 bronze badges 4- 5 developer.mozilla/en/JavaScript/Reference/Global_Objects/… – SLaks Commented May 15, 2012 at 19:48
- 2 possible duplicate of Apply() question for javascript – kapa Commented May 15, 2012 at 19:49
- 4 Was there something unclear about how the method was described when you googled for it? – Jeff Commented May 15, 2012 at 19:50
-
2
Thanks @Jeff. Yes, I don't understand why
init
is defined twice in the code above. The question I asked was for the context in whichapply()
applied. – Shaoz Commented May 15, 2012 at 19:53
1 Answer
Reset to default 9apply
is a member function of a function object. Suppose we have:
function saySomething(thing, anotherThing) {
alert(this + " says " + thing + " and " + anotherThing);
}
Then we can use:
saySomething.apply(document, ["hello", "goodbye"]);
This calls the function and supplies the values of the array as arguments to the function. The first argument species the context of the function (or, what this
equals when the function runs).
You should also be aware that arguments
is a special variable that holds an array of all arguments passed to the function. So, here, this.init.apply(this, arguments)
means that the init
function is called and passed all the of arguments that were passed to the klass
constructor.
In a real implementation, I think init
would expect arguments. Consider how this would be done without apply
:
var klass = function(arg1, arg2, arg3) {
init(arg1, arg2, arg3);
}
klass.prototype.init = function(arg1, arg2, arg3) {}
If you wanted to add arg4
to init, you'd have add it in three places! By having klass
transparently pass all its arguments to init
, you make you code much less brittle.