JavaScript Reusing functions of a prototype

i'm reading the book 'Secrets of the JavaScript Ninja' (http://jsninja.com/) and wonder why a certain code block from one of the examples is coded like it is.

The example (http://jsfiddle.net/3s5bopqe/3/) builds a custom array type (MyArray) and reuses a number of functions from the Array.prototype without actually inheriting from the 'Array' type.

In the example from the book a function with a certain name is reused in the prototype of the custom type using this code

    MyArray.prototype[ name ] = function() {
      return Array.prototype[ name ].apply(this, arguments);
    };

I believe i understand what happens here. But it seems unnecessary to me to actually use a function literal and explicitly 'apply' a function from the Array.prototype. The following code works just as well in the test setting :

 MyArray.prototype[ name ] = Array.prototype[ name ];

My question is whether there are any advantages of using the code from the book over the last code block ?

Answer:1

By using

 MyArray.prototype[ name ] = Array.prototype[ name ];

you are actually copying method from Array into your object (MyArray). In future, if Array method changes, your MyArray method wont be updated.

By using

MyArray.prototype[ name ] = function() {
      return Array.prototype[ name ].apply(this, arguments);
    };

you referred to Array method. Changes made in Array object methods are reflected in your MyArray methods. Why? Because you never copied that method, you just borrowed it (referred to its current implementation).

Answer:2

I am using the following plugin for a datepicker: http://xdsoft.net/jqplugins/datetimepicker/ This is a very nice plugin however I am finding it difficult to set a default date when loading the box. ...

I am using the following plugin for a datepicker: http://xdsoft.net/jqplugins/datetimepicker/ This is a very nice plugin however I am finding it difficult to set a default date when loading the box. ...

I want to perform a custom assertion about the argument passed to a spied-on function call. Can I supply a callback to be used within the expectation against an argument? expect(my.method)....

I want to perform a custom assertion about the argument passed to a spied-on function call. Can I supply a callback to be used within the expectation against an argument? expect(my.method)....

I have an internal form that is web-based. Clients insert their information (Name and such) and sign using a signing pad (This is more than an image. It's the speed, pressure, ect of the signature). ...

I have an internal form that is web-based. Clients insert their information (Name and such) and sign using a signing pad (This is more than an image. It's the speed, pressure, ect of the signature). ...

I have a an array of objects like this in angular: $scope.data = [ {name:"John", group:"a"}, {name:"David", group:"a"}, {name:"Tom", group:"b"}, ]; I want to present this data as ...

I have a an array of objects like this in angular: $scope.data = [ {name:"John", group:"a"}, {name:"David", group:"a"}, {name:"Tom", group:"b"}, ]; I want to present this data as ...

  1. data manipulation angularjs