When running diff
for the first time, it will only alert "Not Supported"
, install the filter
polyfill but do nothing. It just returns undefined
, instead of diffing like it is supposed to do.
Remove the else
, or better just move the filter
installation outside the diff
function - it is not to be a part of it (e.g. in terms of closure, though modern engines will care about this).
Also, IE8 does not support the indexOf
method, you will need to put this compat shim in as well.
if (!Array.prototype.indexOf)
Array.prototype.indexOf = function (searchElement) {…};
if (!Array.prototype.filter)
Array.prototype.filter = function(fun) {…};
Array.prototype.diff = function(a) {
return this.filter(function(i) {
return !(a.indexOf(i) > -1);
});
};
function calculateDiff() {
var arr1 = new Array(1, 2, 3);
var arr2 = new Array(3, 4, 5);
var res = arr1.diff(arr2);
alert(res);
}