John Gnazzo
John Gnazzo

Reputation: 41

Function not working in Internet Explorer 11

I have created a search function that operates on a JavaScript filter function as follows:

result = machinePrinters.filter (search, query);

Search function follows:

function search(user) {
    return Object.keys(this).every((key) => user[key] === this[key]);
}

It works find in Chrome, however in IE 11 I get an error on the => symbol. The error is:

SCRIPT1002: Syntax error

I tried this and id did not work.

function search(user) {
    return Object.keys(this).every(function (key) { return user[key] === this[key]; });
}

Upvotes: 1

Views: 2112

Answers (1)

dfsq
dfsq

Reputation: 193291

If you rewrite your function to use non-arrow function expression then you need to make sure the scope is still points to the same context:

function search(user) {
    return Object.keys(this).every(function (key) { 
        return user[key] === this[key];
    }.bind(this));
}

Using Function.prototype.bind above we make sure that event callback function will be invoked in context the search function is being run in, same as it was before with arrow function (in your case it looks like it's machinePrinters object).

Upvotes: 1

Related Questions