-1

over the last couple days I have been studying some array methods and have seen stuff like [].splice.apply and [].push.apply with some parameters.

Now I think i understand splice for example

var tuna = ['bob', 'john' , 'papa' , 'greg'];
var newArr = []
tuna.splice(0,2,"hello");
console.log(tuna);

will output hello,papa,greg, since it deletes 2 terms in the 0th indice.

I also know how apply works in the following context

var person = {
    firstname: 'John',
    lastname: 'Doe',
    getFullName: function() {

        var fullname = this.firstname + ' ' + this.lastname;
        return fullname;

    }
}

var logName = function(lang1, lang2) {

    console.log('Logged: ' + this.getFullName());
    console.log('Arguments: ' + lang1 + ' ' + lang2);
    console.log('-----------');

}
var logPersonName = logName.bind(person);
logPersonName('en');
logName.call(person, 'en', 'es');
logName.apply(person, ['en', 'es']);

So what happens when we combine these two methods, or push with apply ? A simple example would really help.

Muntasir Alam
  • 1,777
  • 2
  • 17
  • 26
  • 2
    `[].splice.push` does not make sense. `[].splice.apply` does, because "apply" is the name of a function on the Function prototype. Now, `[].push.apply` makes sense, for a similar reason. – Pointy Jun 29 '16 at 15:44
  • Yes, sorry for some reason I typed that without thinking. Thanks for the correction – Muntasir Alam Jun 29 '16 at 15:47

1 Answers1

1

What you've likely seen is:

var foo = Array.prototype.slice.apply(arguments)

This takes the slice method of Array, i.e. the canonical implementation of slice, and uses it on an array-like object, and returns an array. This is a shorthand to turn an array-like object into an actual array. It is purely used because it is the shortest and most convenient way to achieve this goal. slice happens to work on any array-like object and return an actual array, and when not using any of its additional parameters it doesn't do anything else with the array; that's why it's convenient to use for this purpose. It's really abusing the slice method for a purpose it wasn't designed for, but since it works everywhere... well, here we are using it.

Example:

var o = {};
o[0] = 'foo';
o[1] = 'bar';
o.length = 2;

This is an array-like object, but it doesn't have any actual array methods. I can't do o.push('baz'), because o is not an actual array. To turn it into an actual array, I'd use the above method, and can then treat it like an actual array.

deceze
  • 510,633
  • 85
  • 743
  • 889
  • Thanks,do you have any examples of using .splice.apply? – Muntasir Alam Jun 29 '16 at 15:50
  • That's not a typical use I'm aware of, but I'm sure there are situations where it's handy. Come up with your own made-up scenarios. – deceze Jun 29 '16 at 15:51
  • By the way is the way you are explaining "apply" here, different from the example I made? I've only learned to use it in that context – Muntasir Alam Jun 29 '16 at 15:51
  • It's not fundamentally different, no. The key part to understand is that functions and objects are very loosely bound together. Even if a function is defined on an object (usually called a "method"), you can take that function and apply it to anything else you want, it doesn't have to be that original object. That is all simply decided at *call time*, not definition time. `apply` is one way to do this, to use an existing function, regardless of where it comes from, and, well, *apply* it to something else. – deceze Jun 29 '16 at 15:55