2

I'm learning apply() in MDN, there's an example I can't figure out:

// Create constructor method for array of args
Function.prototype.construct = function(aArgs) {
  var oNew = Object.create(this.prototype);
  this.apply(oNew, aArgs);
  return oNew;
};

what does this.apply(oNew, aArgs) do?

2
  • The first argument in apply is the scope and the second, and subsequent arguments, are the parameters being passed to the function which you're applying. Commented Jan 22, 2019 at 9:28
  • @Baruch Actually, the first argument to apply is the binding, not the scope. Scope is a completely different concept Commented Jan 22, 2019 at 9:50

2 Answers 2

4

First: what would be the aim of this function?

The aim

This prototype function constructor is defined to provide an alternative to new. Where new Person(a,b) will need the arguments as such, this function provides a way to pass those arguments as array.

As a side note: it should be said that the spread syntax makes such a feature unnecessary, as we can pass such an array like so: new Person(...[a,b]). But OK...

Example use

Let's say we have a constructor:

function Person(name, gender) {
    this.name = name;
    this.gender = gender;
}

And I have an array with the arguments:

var args = ["Helen", "F"];

Then we could do:

var person = new Person(args[0], args[1]);

But we want to pass args as single argument. Now this prototype function will help us with that. We can now do:

var person = Person.construct(args);

// Create constructor method for array of args
Function.prototype.construct = function(aArgs) {
  var oNew = Object.create(this.prototype);
  this.apply(oNew, aArgs);
  return oNew;
};

function Person(name, gender) {
    this.name = name;
    this.gender = gender;
}

var args = ["Helen", "F"];

var person = Person.construct(args);

console.log(person);

How does it work

When calling Person.construct(args) the this object will be Person, i.e. our constructor function.

Then the following is done:

var oNew = Object.create(this.prototype);

This will create a Person object, but without actually calling the constructor function Person, so the object is not initialised. It is just an empty object that is an instance of Person.

So we need somehow to call Person with the arguments we have. We can use apply for that:

this.apply(oNew, aArgs);

Recall that this is Person, so we call Person here via apply. apply makes it possible to call a function with a specific this object value (our newly created instance of Person) and the arguments as an array -- exactly what we need.

So the above will initialise our empty object with the name and gender arguments, completing the full creation and initialisation that you would normally have with new.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for your time and splendid explanation! I think the key point here is the customized construct() will always be used with ordinary constructor function like Foo.construct() so this will refer to Foo.
Best explain ever !! Its the demo code from @trincot make all things easier for our brain, thanks u very much
1

This function is works like the operator new in Javascript.
You can figure out how new works in MDN
For example, if you define an constructor function like below

function Foo(name, age) {
  this.name = name
  this.age = age
}

Foo.prototype.hello = function() {
  console.log('hello' + this.name + this.age)
}

If you already define the Function.prototype.construct like the example you show.
Then you can use either const a = new Foo('a', 1) or const a = Foo.construct('a', 1) to create the same object

And this.apply(oNew, aArgs) is used for call the constructor function to initiate the instance just created

1 Comment

Thank you for your time and effort buddy! Much appreciated.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.