How to Polyfill Array.prototype.includes() for IE8
I'm trying to polyfill the array method includes() to work with IE8, which I need to support for a project and I don't want to use indexOf().
I know there's a polyfill, so I went to:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes#Polyfill
And included it at the top of my script.
IE8 has limited support for Object.defineProperty() so I polyfilled it as well:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperties#Polyfill
Lastly, I needed to polyfill Object.keys():
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys#Polyfill
Put together, my polyfills are:
if (!Object.keys)
Object.keys = (function()
'use strict';
var hasOwnProperty = Object.prototype.hasOwnProperty,
hasDontEnumBug = !( toString: null ).propertyIsEnumerable('toString'),
dontEnums = [
'toString',
'toLocaleString',
'valueOf',
'hasOwnProperty',
'isPrototypeOf',
'propertyIsEnumerable',
'constructor'
],
dontEnumsLength = dontEnums.length;
return function(obj)
if (typeof obj !== 'function' && (typeof obj !== 'object' ;
());
function defineProperties(obj, properties)
function convertToDescriptor(desc) desc === null)
throw new TypeError('bad desc');
var d = ;
if (hasProperty(desc, 'enumerable'))
d.enumerable = !!desc.enumerable;
if (hasProperty(desc, 'configurable'))
d.configurable = !!desc.configurable;
if (hasProperty(desc, 'value'))
d.value = desc.value;
if (hasProperty(desc, 'writable'))
d.writable = !!desc.writable;
if (hasProperty(desc, 'get'))
var g = desc.get;
if (!isCallable(g) && typeof g !== 'undefined')
throw new TypeError('bad get');
d.get = g;
if (hasProperty(desc, 'set'))
var s = desc.set;
if (!isCallable(s) && typeof s !== 'undefined')
throw new TypeError('bad set');
d.set = s;
if (('get' in d
if (typeof obj !== 'object'
if (!Array.prototype.includes)
alert('test');
defineProperties(Array.prototype, 'includes',
value: function(searchElement, fromIndex) 0;
// 5. If n ≥ 0, then
// a. Let k be n.
// 6. Else n < 0,
// a. Let k be len + n.
// b. If k < 0, let k be 0.
var k = Math.max(n >= 0 ? n : len - Math.abs(n), 0);
function sameValueZero(x, y)
return x === y
// 7. Repeat, while k < len
while (k < len)
// a. Let elementK be the result of ? Get(O, ! ToString(k)).
// b. If SameValueZero(searchElement, elementK) is true, return true.
if (sameValueZero(o[k], searchElement))
return true;
// c. Increase k by 1.
k++;
// 8. Return false
return false;
);
To test the above script, I did:
var arr = ["one", "two", "three"];
alert( arr.includes('two') );
IE8 throws this error:
Object doesn't support this property or method.
I thought that by using polyfills, I would be able to add support for array includes() to IE8. What am I doing wrong? Any corrections or links to other polyfills are welcomed. No jQuery or other libraries. I just want to add support for includes() to IE.
javascript polyfills
add a comment |
I'm trying to polyfill the array method includes() to work with IE8, which I need to support for a project and I don't want to use indexOf().
I know there's a polyfill, so I went to:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes#Polyfill
And included it at the top of my script.
IE8 has limited support for Object.defineProperty() so I polyfilled it as well:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperties#Polyfill
Lastly, I needed to polyfill Object.keys():
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys#Polyfill
Put together, my polyfills are:
if (!Object.keys)
Object.keys = (function()
'use strict';
var hasOwnProperty = Object.prototype.hasOwnProperty,
hasDontEnumBug = !( toString: null ).propertyIsEnumerable('toString'),
dontEnums = [
'toString',
'toLocaleString',
'valueOf',
'hasOwnProperty',
'isPrototypeOf',
'propertyIsEnumerable',
'constructor'
],
dontEnumsLength = dontEnums.length;
return function(obj)
if (typeof obj !== 'function' && (typeof obj !== 'object' ;
());
function defineProperties(obj, properties)
function convertToDescriptor(desc) desc === null)
throw new TypeError('bad desc');
var d = ;
if (hasProperty(desc, 'enumerable'))
d.enumerable = !!desc.enumerable;
if (hasProperty(desc, 'configurable'))
d.configurable = !!desc.configurable;
if (hasProperty(desc, 'value'))
d.value = desc.value;
if (hasProperty(desc, 'writable'))
d.writable = !!desc.writable;
if (hasProperty(desc, 'get'))
var g = desc.get;
if (!isCallable(g) && typeof g !== 'undefined')
throw new TypeError('bad get');
d.get = g;
if (hasProperty(desc, 'set'))
var s = desc.set;
if (!isCallable(s) && typeof s !== 'undefined')
throw new TypeError('bad set');
d.set = s;
if (('get' in d
if (typeof obj !== 'object'
if (!Array.prototype.includes)
alert('test');
defineProperties(Array.prototype, 'includes',
value: function(searchElement, fromIndex) 0;
// 5. If n ≥ 0, then
// a. Let k be n.
// 6. Else n < 0,
// a. Let k be len + n.
// b. If k < 0, let k be 0.
var k = Math.max(n >= 0 ? n : len - Math.abs(n), 0);
function sameValueZero(x, y)
return x === y
// 7. Repeat, while k < len
while (k < len)
// a. Let elementK be the result of ? Get(O, ! ToString(k)).
// b. If SameValueZero(searchElement, elementK) is true, return true.
if (sameValueZero(o[k], searchElement))
return true;
// c. Increase k by 1.
k++;
// 8. Return false
return false;
);
To test the above script, I did:
var arr = ["one", "two", "three"];
alert( arr.includes('two') );
IE8 throws this error:
Object doesn't support this property or method.
I thought that by using polyfills, I would be able to add support for array includes() to IE8. What am I doing wrong? Any corrections or links to other polyfills are welcomed. No jQuery or other libraries. I just want to add support for includes() to IE.
javascript polyfills
This should work if you only include the 'includes' polyfill. I suspect the problem lies somewhere in the order you have included all three
– Katie.Sun
Nov 14 '18 at 20:50
What about using cdn.polyfill.io/v2/polyfill.min.js from polyfill.io/v2/docs automatically detects what is missing in your browser =)
– Gabriel Lopez
Nov 14 '18 at 20:56
Why can't you useindexOf? What's the reasoning?
– Zze
Nov 14 '18 at 21:00
@Zze The main reason is I'm trying to learn how to use polyfills.
– GTS Joe
Nov 14 '18 at 21:03
@GTSJoe then polyfill.includeswithindexOf? Polyfills are not a set implementation. There are many different implementations for polyfilling the same functions.
– Zze
Nov 14 '18 at 21:23
add a comment |
I'm trying to polyfill the array method includes() to work with IE8, which I need to support for a project and I don't want to use indexOf().
I know there's a polyfill, so I went to:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes#Polyfill
And included it at the top of my script.
IE8 has limited support for Object.defineProperty() so I polyfilled it as well:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperties#Polyfill
Lastly, I needed to polyfill Object.keys():
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys#Polyfill
Put together, my polyfills are:
if (!Object.keys)
Object.keys = (function()
'use strict';
var hasOwnProperty = Object.prototype.hasOwnProperty,
hasDontEnumBug = !( toString: null ).propertyIsEnumerable('toString'),
dontEnums = [
'toString',
'toLocaleString',
'valueOf',
'hasOwnProperty',
'isPrototypeOf',
'propertyIsEnumerable',
'constructor'
],
dontEnumsLength = dontEnums.length;
return function(obj)
if (typeof obj !== 'function' && (typeof obj !== 'object' ;
());
function defineProperties(obj, properties)
function convertToDescriptor(desc) desc === null)
throw new TypeError('bad desc');
var d = ;
if (hasProperty(desc, 'enumerable'))
d.enumerable = !!desc.enumerable;
if (hasProperty(desc, 'configurable'))
d.configurable = !!desc.configurable;
if (hasProperty(desc, 'value'))
d.value = desc.value;
if (hasProperty(desc, 'writable'))
d.writable = !!desc.writable;
if (hasProperty(desc, 'get'))
var g = desc.get;
if (!isCallable(g) && typeof g !== 'undefined')
throw new TypeError('bad get');
d.get = g;
if (hasProperty(desc, 'set'))
var s = desc.set;
if (!isCallable(s) && typeof s !== 'undefined')
throw new TypeError('bad set');
d.set = s;
if (('get' in d
if (typeof obj !== 'object'
if (!Array.prototype.includes)
alert('test');
defineProperties(Array.prototype, 'includes',
value: function(searchElement, fromIndex) 0;
// 5. If n ≥ 0, then
// a. Let k be n.
// 6. Else n < 0,
// a. Let k be len + n.
// b. If k < 0, let k be 0.
var k = Math.max(n >= 0 ? n : len - Math.abs(n), 0);
function sameValueZero(x, y)
return x === y
// 7. Repeat, while k < len
while (k < len)
// a. Let elementK be the result of ? Get(O, ! ToString(k)).
// b. If SameValueZero(searchElement, elementK) is true, return true.
if (sameValueZero(o[k], searchElement))
return true;
// c. Increase k by 1.
k++;
// 8. Return false
return false;
);
To test the above script, I did:
var arr = ["one", "two", "three"];
alert( arr.includes('two') );
IE8 throws this error:
Object doesn't support this property or method.
I thought that by using polyfills, I would be able to add support for array includes() to IE8. What am I doing wrong? Any corrections or links to other polyfills are welcomed. No jQuery or other libraries. I just want to add support for includes() to IE.
javascript polyfills
I'm trying to polyfill the array method includes() to work with IE8, which I need to support for a project and I don't want to use indexOf().
I know there's a polyfill, so I went to:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes#Polyfill
And included it at the top of my script.
IE8 has limited support for Object.defineProperty() so I polyfilled it as well:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperties#Polyfill
Lastly, I needed to polyfill Object.keys():
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys#Polyfill
Put together, my polyfills are:
if (!Object.keys)
Object.keys = (function()
'use strict';
var hasOwnProperty = Object.prototype.hasOwnProperty,
hasDontEnumBug = !( toString: null ).propertyIsEnumerable('toString'),
dontEnums = [
'toString',
'toLocaleString',
'valueOf',
'hasOwnProperty',
'isPrototypeOf',
'propertyIsEnumerable',
'constructor'
],
dontEnumsLength = dontEnums.length;
return function(obj)
if (typeof obj !== 'function' && (typeof obj !== 'object' ;
());
function defineProperties(obj, properties)
function convertToDescriptor(desc) desc === null)
throw new TypeError('bad desc');
var d = ;
if (hasProperty(desc, 'enumerable'))
d.enumerable = !!desc.enumerable;
if (hasProperty(desc, 'configurable'))
d.configurable = !!desc.configurable;
if (hasProperty(desc, 'value'))
d.value = desc.value;
if (hasProperty(desc, 'writable'))
d.writable = !!desc.writable;
if (hasProperty(desc, 'get'))
var g = desc.get;
if (!isCallable(g) && typeof g !== 'undefined')
throw new TypeError('bad get');
d.get = g;
if (hasProperty(desc, 'set'))
var s = desc.set;
if (!isCallable(s) && typeof s !== 'undefined')
throw new TypeError('bad set');
d.set = s;
if (('get' in d
if (typeof obj !== 'object'
if (!Array.prototype.includes)
alert('test');
defineProperties(Array.prototype, 'includes',
value: function(searchElement, fromIndex) 0;
// 5. If n ≥ 0, then
// a. Let k be n.
// 6. Else n < 0,
// a. Let k be len + n.
// b. If k < 0, let k be 0.
var k = Math.max(n >= 0 ? n : len - Math.abs(n), 0);
function sameValueZero(x, y)
return x === y
// 7. Repeat, while k < len
while (k < len)
// a. Let elementK be the result of ? Get(O, ! ToString(k)).
// b. If SameValueZero(searchElement, elementK) is true, return true.
if (sameValueZero(o[k], searchElement))
return true;
// c. Increase k by 1.
k++;
// 8. Return false
return false;
);
To test the above script, I did:
var arr = ["one", "two", "three"];
alert( arr.includes('two') );
IE8 throws this error:
Object doesn't support this property or method.
I thought that by using polyfills, I would be able to add support for array includes() to IE8. What am I doing wrong? Any corrections or links to other polyfills are welcomed. No jQuery or other libraries. I just want to add support for includes() to IE.
javascript polyfills
javascript polyfills
edited Nov 14 '18 at 20:44
GTS Joe
asked Nov 14 '18 at 20:39
GTS JoeGTS Joe
59311333
59311333
This should work if you only include the 'includes' polyfill. I suspect the problem lies somewhere in the order you have included all three
– Katie.Sun
Nov 14 '18 at 20:50
What about using cdn.polyfill.io/v2/polyfill.min.js from polyfill.io/v2/docs automatically detects what is missing in your browser =)
– Gabriel Lopez
Nov 14 '18 at 20:56
Why can't you useindexOf? What's the reasoning?
– Zze
Nov 14 '18 at 21:00
@Zze The main reason is I'm trying to learn how to use polyfills.
– GTS Joe
Nov 14 '18 at 21:03
@GTSJoe then polyfill.includeswithindexOf? Polyfills are not a set implementation. There are many different implementations for polyfilling the same functions.
– Zze
Nov 14 '18 at 21:23
add a comment |
This should work if you only include the 'includes' polyfill. I suspect the problem lies somewhere in the order you have included all three
– Katie.Sun
Nov 14 '18 at 20:50
What about using cdn.polyfill.io/v2/polyfill.min.js from polyfill.io/v2/docs automatically detects what is missing in your browser =)
– Gabriel Lopez
Nov 14 '18 at 20:56
Why can't you useindexOf? What's the reasoning?
– Zze
Nov 14 '18 at 21:00
@Zze The main reason is I'm trying to learn how to use polyfills.
– GTS Joe
Nov 14 '18 at 21:03
@GTSJoe then polyfill.includeswithindexOf? Polyfills are not a set implementation. There are many different implementations for polyfilling the same functions.
– Zze
Nov 14 '18 at 21:23
This should work if you only include the 'includes' polyfill. I suspect the problem lies somewhere in the order you have included all three
– Katie.Sun
Nov 14 '18 at 20:50
This should work if you only include the 'includes' polyfill. I suspect the problem lies somewhere in the order you have included all three
– Katie.Sun
Nov 14 '18 at 20:50
What about using cdn.polyfill.io/v2/polyfill.min.js from polyfill.io/v2/docs automatically detects what is missing in your browser =)
– Gabriel Lopez
Nov 14 '18 at 20:56
What about using cdn.polyfill.io/v2/polyfill.min.js from polyfill.io/v2/docs automatically detects what is missing in your browser =)
– Gabriel Lopez
Nov 14 '18 at 20:56
Why can't you use
indexOf ? What's the reasoning?– Zze
Nov 14 '18 at 21:00
Why can't you use
indexOf ? What's the reasoning?– Zze
Nov 14 '18 at 21:00
@Zze The main reason is I'm trying to learn how to use polyfills.
– GTS Joe
Nov 14 '18 at 21:03
@Zze The main reason is I'm trying to learn how to use polyfills.
– GTS Joe
Nov 14 '18 at 21:03
@GTSJoe then polyfill
.includes with indexOf? Polyfills are not a set implementation. There are many different implementations for polyfilling the same functions.– Zze
Nov 14 '18 at 21:23
@GTSJoe then polyfill
.includes with indexOf? Polyfills are not a set implementation. There are many different implementations for polyfilling the same functions.– Zze
Nov 14 '18 at 21:23
add a comment |
3 Answers
3
active
oldest
votes
use arr.indexOf('two')
if return 1 is because it exists, and -1 does not exist
I'm avoiding indexOf() as stated in the intro, to learn how to use polyfills.
– GTS Joe
Nov 14 '18 at 21:06
add a comment |
Unfortunately I don't have a way to test in IE8, but I think there are a couple of things going on here -- first, you don't need to define defineProperties(obj, properties) because neither the Object.keys nor Array.prototype.includes polyfills use it -- they are both self-contained. The fact that they are self-contained also means that you don't need to include the Object.keys polyfill.
I would suggest only using the includes Array.prototype.includes if that is all you need.
1
Katie, you can use a virtual machine to test old browsers like IE8. I'm using VirtualBox by Oracle, which I downloaded for free at: virtualbox.org/wiki/Downloads then, I downloaded the actual virtual machine image running Windows 7 with IE8 from: developer.microsoft.com/en-us/microsoft-edge/tools/vms Check it out! It'll help you out as a developer.
– GTS Joe
Nov 14 '18 at 21:30
1
@GTSJoe Thanks! I take it the above didn't help with the polyfill error?
– Katie.Sun
Nov 14 '18 at 21:38
I tried your suggestion, but it didn't work. Thanks for trying though!
– GTS Joe
Nov 14 '18 at 21:43
add a comment |
You can simply use Array#indexOf to polyfill Array#includes.
For IE8, you will also need to polyfill Array#indexOf as it was only implemented in IE9.
if(!Array.prototype.includes)
//or use Object.defineProperty
Array.prototype.includes = function(search)
return !!~this.indexOf(search);
if(!Array.prototype.indexOf)
Array.prototype.indexOf = (function(Object, max, min)
"use strict";
return function indexOf(member, fromIndex) this===undefined)throw TypeError("Array.prototype.indexOf called on null or undefined");
var that = Object(this), Len = that.length >>> 0, i = min(fromIndex ;
)(Object, Math.max, Math.min);
Array.prototype.includes = null;
Array.prototype.indexOf = null;
if(!Array.prototype.includes)
console.log('Polyfilled Array.prototype.includes');
//or use Object.defineProperty
Array.prototype.includes = function(search)
return !!~this.indexOf(search);
if(!Array.prototype.indexOf)
console.log("Polyfilled Array.prototype.indexOf");
Array.prototype.indexOf = (function(Object, max, min)
"use strict";
return function indexOf(member, fromIndex)
if(this===null;
)(Object, Math.max, Math.min);
var obj = x: 1, y: 2;
var arr = [1, 'what', obj];
console.log('Array includes 1:',arr.includes(1));
console.log('Array includes 'what':', arr.includes('what'));
console.log('Array includes obj:', arr.includes(obj));
console.log('Array inclues 3:', arr.includes(3));
Tested it, I get: Object doesn't support this property or method.
– GTS Joe
Nov 14 '18 at 21:33
@GTSJoe I added a code snippet. Try it out!
– hev1
Nov 14 '18 at 21:40
Pretty sure you don't need the double!!~this.indexOf(search);should suffice?
– Zze
Nov 14 '18 at 21:42
@hev1 I tried your code, but still get "Object doesn't support this property or method." Try it in IE8. It works fine in the snippet, but IE8 is another story.
– GTS Joe
Nov 14 '18 at 21:46
@Zze It is necessary to convert the result to a boolean. ~ is a bitwise not. It performs the not operation on each bit of the operand and then returns the result.
– hev1
Nov 14 '18 at 23:09
|
show 3 more comments
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53308396%2fhow-to-polyfill-array-prototype-includes-for-ie8%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
use arr.indexOf('two')
if return 1 is because it exists, and -1 does not exist
I'm avoiding indexOf() as stated in the intro, to learn how to use polyfills.
– GTS Joe
Nov 14 '18 at 21:06
add a comment |
use arr.indexOf('two')
if return 1 is because it exists, and -1 does not exist
I'm avoiding indexOf() as stated in the intro, to learn how to use polyfills.
– GTS Joe
Nov 14 '18 at 21:06
add a comment |
use arr.indexOf('two')
if return 1 is because it exists, and -1 does not exist
use arr.indexOf('two')
if return 1 is because it exists, and -1 does not exist
answered Nov 14 '18 at 20:54
Henrique VianaHenrique Viana
29718
29718
I'm avoiding indexOf() as stated in the intro, to learn how to use polyfills.
– GTS Joe
Nov 14 '18 at 21:06
add a comment |
I'm avoiding indexOf() as stated in the intro, to learn how to use polyfills.
– GTS Joe
Nov 14 '18 at 21:06
I'm avoiding indexOf() as stated in the intro, to learn how to use polyfills.
– GTS Joe
Nov 14 '18 at 21:06
I'm avoiding indexOf() as stated in the intro, to learn how to use polyfills.
– GTS Joe
Nov 14 '18 at 21:06
add a comment |
Unfortunately I don't have a way to test in IE8, but I think there are a couple of things going on here -- first, you don't need to define defineProperties(obj, properties) because neither the Object.keys nor Array.prototype.includes polyfills use it -- they are both self-contained. The fact that they are self-contained also means that you don't need to include the Object.keys polyfill.
I would suggest only using the includes Array.prototype.includes if that is all you need.
1
Katie, you can use a virtual machine to test old browsers like IE8. I'm using VirtualBox by Oracle, which I downloaded for free at: virtualbox.org/wiki/Downloads then, I downloaded the actual virtual machine image running Windows 7 with IE8 from: developer.microsoft.com/en-us/microsoft-edge/tools/vms Check it out! It'll help you out as a developer.
– GTS Joe
Nov 14 '18 at 21:30
1
@GTSJoe Thanks! I take it the above didn't help with the polyfill error?
– Katie.Sun
Nov 14 '18 at 21:38
I tried your suggestion, but it didn't work. Thanks for trying though!
– GTS Joe
Nov 14 '18 at 21:43
add a comment |
Unfortunately I don't have a way to test in IE8, but I think there are a couple of things going on here -- first, you don't need to define defineProperties(obj, properties) because neither the Object.keys nor Array.prototype.includes polyfills use it -- they are both self-contained. The fact that they are self-contained also means that you don't need to include the Object.keys polyfill.
I would suggest only using the includes Array.prototype.includes if that is all you need.
1
Katie, you can use a virtual machine to test old browsers like IE8. I'm using VirtualBox by Oracle, which I downloaded for free at: virtualbox.org/wiki/Downloads then, I downloaded the actual virtual machine image running Windows 7 with IE8 from: developer.microsoft.com/en-us/microsoft-edge/tools/vms Check it out! It'll help you out as a developer.
– GTS Joe
Nov 14 '18 at 21:30
1
@GTSJoe Thanks! I take it the above didn't help with the polyfill error?
– Katie.Sun
Nov 14 '18 at 21:38
I tried your suggestion, but it didn't work. Thanks for trying though!
– GTS Joe
Nov 14 '18 at 21:43
add a comment |
Unfortunately I don't have a way to test in IE8, but I think there are a couple of things going on here -- first, you don't need to define defineProperties(obj, properties) because neither the Object.keys nor Array.prototype.includes polyfills use it -- they are both self-contained. The fact that they are self-contained also means that you don't need to include the Object.keys polyfill.
I would suggest only using the includes Array.prototype.includes if that is all you need.
Unfortunately I don't have a way to test in IE8, but I think there are a couple of things going on here -- first, you don't need to define defineProperties(obj, properties) because neither the Object.keys nor Array.prototype.includes polyfills use it -- they are both self-contained. The fact that they are self-contained also means that you don't need to include the Object.keys polyfill.
I would suggest only using the includes Array.prototype.includes if that is all you need.
answered Nov 14 '18 at 21:14
Katie.SunKatie.Sun
587114
587114
1
Katie, you can use a virtual machine to test old browsers like IE8. I'm using VirtualBox by Oracle, which I downloaded for free at: virtualbox.org/wiki/Downloads then, I downloaded the actual virtual machine image running Windows 7 with IE8 from: developer.microsoft.com/en-us/microsoft-edge/tools/vms Check it out! It'll help you out as a developer.
– GTS Joe
Nov 14 '18 at 21:30
1
@GTSJoe Thanks! I take it the above didn't help with the polyfill error?
– Katie.Sun
Nov 14 '18 at 21:38
I tried your suggestion, but it didn't work. Thanks for trying though!
– GTS Joe
Nov 14 '18 at 21:43
add a comment |
1
Katie, you can use a virtual machine to test old browsers like IE8. I'm using VirtualBox by Oracle, which I downloaded for free at: virtualbox.org/wiki/Downloads then, I downloaded the actual virtual machine image running Windows 7 with IE8 from: developer.microsoft.com/en-us/microsoft-edge/tools/vms Check it out! It'll help you out as a developer.
– GTS Joe
Nov 14 '18 at 21:30
1
@GTSJoe Thanks! I take it the above didn't help with the polyfill error?
– Katie.Sun
Nov 14 '18 at 21:38
I tried your suggestion, but it didn't work. Thanks for trying though!
– GTS Joe
Nov 14 '18 at 21:43
1
1
Katie, you can use a virtual machine to test old browsers like IE8. I'm using VirtualBox by Oracle, which I downloaded for free at: virtualbox.org/wiki/Downloads then, I downloaded the actual virtual machine image running Windows 7 with IE8 from: developer.microsoft.com/en-us/microsoft-edge/tools/vms Check it out! It'll help you out as a developer.
– GTS Joe
Nov 14 '18 at 21:30
Katie, you can use a virtual machine to test old browsers like IE8. I'm using VirtualBox by Oracle, which I downloaded for free at: virtualbox.org/wiki/Downloads then, I downloaded the actual virtual machine image running Windows 7 with IE8 from: developer.microsoft.com/en-us/microsoft-edge/tools/vms Check it out! It'll help you out as a developer.
– GTS Joe
Nov 14 '18 at 21:30
1
1
@GTSJoe Thanks! I take it the above didn't help with the polyfill error?
– Katie.Sun
Nov 14 '18 at 21:38
@GTSJoe Thanks! I take it the above didn't help with the polyfill error?
– Katie.Sun
Nov 14 '18 at 21:38
I tried your suggestion, but it didn't work. Thanks for trying though!
– GTS Joe
Nov 14 '18 at 21:43
I tried your suggestion, but it didn't work. Thanks for trying though!
– GTS Joe
Nov 14 '18 at 21:43
add a comment |
You can simply use Array#indexOf to polyfill Array#includes.
For IE8, you will also need to polyfill Array#indexOf as it was only implemented in IE9.
if(!Array.prototype.includes)
//or use Object.defineProperty
Array.prototype.includes = function(search)
return !!~this.indexOf(search);
if(!Array.prototype.indexOf)
Array.prototype.indexOf = (function(Object, max, min)
"use strict";
return function indexOf(member, fromIndex) this===undefined)throw TypeError("Array.prototype.indexOf called on null or undefined");
var that = Object(this), Len = that.length >>> 0, i = min(fromIndex ;
)(Object, Math.max, Math.min);
Array.prototype.includes = null;
Array.prototype.indexOf = null;
if(!Array.prototype.includes)
console.log('Polyfilled Array.prototype.includes');
//or use Object.defineProperty
Array.prototype.includes = function(search)
return !!~this.indexOf(search);
if(!Array.prototype.indexOf)
console.log("Polyfilled Array.prototype.indexOf");
Array.prototype.indexOf = (function(Object, max, min)
"use strict";
return function indexOf(member, fromIndex)
if(this===null;
)(Object, Math.max, Math.min);
var obj = x: 1, y: 2;
var arr = [1, 'what', obj];
console.log('Array includes 1:',arr.includes(1));
console.log('Array includes 'what':', arr.includes('what'));
console.log('Array includes obj:', arr.includes(obj));
console.log('Array inclues 3:', arr.includes(3));
Tested it, I get: Object doesn't support this property or method.
– GTS Joe
Nov 14 '18 at 21:33
@GTSJoe I added a code snippet. Try it out!
– hev1
Nov 14 '18 at 21:40
Pretty sure you don't need the double!!~this.indexOf(search);should suffice?
– Zze
Nov 14 '18 at 21:42
@hev1 I tried your code, but still get "Object doesn't support this property or method." Try it in IE8. It works fine in the snippet, but IE8 is another story.
– GTS Joe
Nov 14 '18 at 21:46
@Zze It is necessary to convert the result to a boolean. ~ is a bitwise not. It performs the not operation on each bit of the operand and then returns the result.
– hev1
Nov 14 '18 at 23:09
|
show 3 more comments
You can simply use Array#indexOf to polyfill Array#includes.
For IE8, you will also need to polyfill Array#indexOf as it was only implemented in IE9.
if(!Array.prototype.includes)
//or use Object.defineProperty
Array.prototype.includes = function(search)
return !!~this.indexOf(search);
if(!Array.prototype.indexOf)
Array.prototype.indexOf = (function(Object, max, min)
"use strict";
return function indexOf(member, fromIndex) this===undefined)throw TypeError("Array.prototype.indexOf called on null or undefined");
var that = Object(this), Len = that.length >>> 0, i = min(fromIndex ;
)(Object, Math.max, Math.min);
Array.prototype.includes = null;
Array.prototype.indexOf = null;
if(!Array.prototype.includes)
console.log('Polyfilled Array.prototype.includes');
//or use Object.defineProperty
Array.prototype.includes = function(search)
return !!~this.indexOf(search);
if(!Array.prototype.indexOf)
console.log("Polyfilled Array.prototype.indexOf");
Array.prototype.indexOf = (function(Object, max, min)
"use strict";
return function indexOf(member, fromIndex)
if(this===null;
)(Object, Math.max, Math.min);
var obj = x: 1, y: 2;
var arr = [1, 'what', obj];
console.log('Array includes 1:',arr.includes(1));
console.log('Array includes 'what':', arr.includes('what'));
console.log('Array includes obj:', arr.includes(obj));
console.log('Array inclues 3:', arr.includes(3));
Tested it, I get: Object doesn't support this property or method.
– GTS Joe
Nov 14 '18 at 21:33
@GTSJoe I added a code snippet. Try it out!
– hev1
Nov 14 '18 at 21:40
Pretty sure you don't need the double!!~this.indexOf(search);should suffice?
– Zze
Nov 14 '18 at 21:42
@hev1 I tried your code, but still get "Object doesn't support this property or method." Try it in IE8. It works fine in the snippet, but IE8 is another story.
– GTS Joe
Nov 14 '18 at 21:46
@Zze It is necessary to convert the result to a boolean. ~ is a bitwise not. It performs the not operation on each bit of the operand and then returns the result.
– hev1
Nov 14 '18 at 23:09
|
show 3 more comments
You can simply use Array#indexOf to polyfill Array#includes.
For IE8, you will also need to polyfill Array#indexOf as it was only implemented in IE9.
if(!Array.prototype.includes)
//or use Object.defineProperty
Array.prototype.includes = function(search)
return !!~this.indexOf(search);
if(!Array.prototype.indexOf)
Array.prototype.indexOf = (function(Object, max, min)
"use strict";
return function indexOf(member, fromIndex) this===undefined)throw TypeError("Array.prototype.indexOf called on null or undefined");
var that = Object(this), Len = that.length >>> 0, i = min(fromIndex ;
)(Object, Math.max, Math.min);
Array.prototype.includes = null;
Array.prototype.indexOf = null;
if(!Array.prototype.includes)
console.log('Polyfilled Array.prototype.includes');
//or use Object.defineProperty
Array.prototype.includes = function(search)
return !!~this.indexOf(search);
if(!Array.prototype.indexOf)
console.log("Polyfilled Array.prototype.indexOf");
Array.prototype.indexOf = (function(Object, max, min)
"use strict";
return function indexOf(member, fromIndex)
if(this===null;
)(Object, Math.max, Math.min);
var obj = x: 1, y: 2;
var arr = [1, 'what', obj];
console.log('Array includes 1:',arr.includes(1));
console.log('Array includes 'what':', arr.includes('what'));
console.log('Array includes obj:', arr.includes(obj));
console.log('Array inclues 3:', arr.includes(3));You can simply use Array#indexOf to polyfill Array#includes.
For IE8, you will also need to polyfill Array#indexOf as it was only implemented in IE9.
if(!Array.prototype.includes)
//or use Object.defineProperty
Array.prototype.includes = function(search)
return !!~this.indexOf(search);
if(!Array.prototype.indexOf)
Array.prototype.indexOf = (function(Object, max, min)
"use strict";
return function indexOf(member, fromIndex) this===undefined)throw TypeError("Array.prototype.indexOf called on null or undefined");
var that = Object(this), Len = that.length >>> 0, i = min(fromIndex ;
)(Object, Math.max, Math.min);
Array.prototype.includes = null;
Array.prototype.indexOf = null;
if(!Array.prototype.includes)
console.log('Polyfilled Array.prototype.includes');
//or use Object.defineProperty
Array.prototype.includes = function(search)
return !!~this.indexOf(search);
if(!Array.prototype.indexOf)
console.log("Polyfilled Array.prototype.indexOf");
Array.prototype.indexOf = (function(Object, max, min)
"use strict";
return function indexOf(member, fromIndex)
if(this===null;
)(Object, Math.max, Math.min);
var obj = x: 1, y: 2;
var arr = [1, 'what', obj];
console.log('Array includes 1:',arr.includes(1));
console.log('Array includes 'what':', arr.includes('what'));
console.log('Array includes obj:', arr.includes(obj));
console.log('Array inclues 3:', arr.includes(3));Array.prototype.includes = null;
Array.prototype.indexOf = null;
if(!Array.prototype.includes)
console.log('Polyfilled Array.prototype.includes');
//or use Object.defineProperty
Array.prototype.includes = function(search)
return !!~this.indexOf(search);
if(!Array.prototype.indexOf)
console.log("Polyfilled Array.prototype.indexOf");
Array.prototype.indexOf = (function(Object, max, min)
"use strict";
return function indexOf(member, fromIndex)
if(this===null;
)(Object, Math.max, Math.min);
var obj = x: 1, y: 2;
var arr = [1, 'what', obj];
console.log('Array includes 1:',arr.includes(1));
console.log('Array includes 'what':', arr.includes('what'));
console.log('Array includes obj:', arr.includes(obj));
console.log('Array inclues 3:', arr.includes(3));Array.prototype.includes = null;
Array.prototype.indexOf = null;
if(!Array.prototype.includes)
console.log('Polyfilled Array.prototype.includes');
//or use Object.defineProperty
Array.prototype.includes = function(search)
return !!~this.indexOf(search);
if(!Array.prototype.indexOf)
console.log("Polyfilled Array.prototype.indexOf");
Array.prototype.indexOf = (function(Object, max, min)
"use strict";
return function indexOf(member, fromIndex)
if(this===null;
)(Object, Math.max, Math.min);
var obj = x: 1, y: 2;
var arr = [1, 'what', obj];
console.log('Array includes 1:',arr.includes(1));
console.log('Array includes 'what':', arr.includes('what'));
console.log('Array includes obj:', arr.includes(obj));
console.log('Array inclues 3:', arr.includes(3));edited Nov 15 '18 at 1:32
answered Nov 14 '18 at 21:28
hev1hev1
5,9383729
5,9383729
Tested it, I get: Object doesn't support this property or method.
– GTS Joe
Nov 14 '18 at 21:33
@GTSJoe I added a code snippet. Try it out!
– hev1
Nov 14 '18 at 21:40
Pretty sure you don't need the double!!~this.indexOf(search);should suffice?
– Zze
Nov 14 '18 at 21:42
@hev1 I tried your code, but still get "Object doesn't support this property or method." Try it in IE8. It works fine in the snippet, but IE8 is another story.
– GTS Joe
Nov 14 '18 at 21:46
@Zze It is necessary to convert the result to a boolean. ~ is a bitwise not. It performs the not operation on each bit of the operand and then returns the result.
– hev1
Nov 14 '18 at 23:09
|
show 3 more comments
Tested it, I get: Object doesn't support this property or method.
– GTS Joe
Nov 14 '18 at 21:33
@GTSJoe I added a code snippet. Try it out!
– hev1
Nov 14 '18 at 21:40
Pretty sure you don't need the double!!~this.indexOf(search);should suffice?
– Zze
Nov 14 '18 at 21:42
@hev1 I tried your code, but still get "Object doesn't support this property or method." Try it in IE8. It works fine in the snippet, but IE8 is another story.
– GTS Joe
Nov 14 '18 at 21:46
@Zze It is necessary to convert the result to a boolean. ~ is a bitwise not. It performs the not operation on each bit of the operand and then returns the result.
– hev1
Nov 14 '18 at 23:09
Tested it, I get: Object doesn't support this property or method.
– GTS Joe
Nov 14 '18 at 21:33
Tested it, I get: Object doesn't support this property or method.
– GTS Joe
Nov 14 '18 at 21:33
@GTSJoe I added a code snippet. Try it out!
– hev1
Nov 14 '18 at 21:40
@GTSJoe I added a code snippet. Try it out!
– hev1
Nov 14 '18 at 21:40
Pretty sure you don't need the double
!! ~this.indexOf(search); should suffice?– Zze
Nov 14 '18 at 21:42
Pretty sure you don't need the double
!! ~this.indexOf(search); should suffice?– Zze
Nov 14 '18 at 21:42
@hev1 I tried your code, but still get "Object doesn't support this property or method." Try it in IE8. It works fine in the snippet, but IE8 is another story.
– GTS Joe
Nov 14 '18 at 21:46
@hev1 I tried your code, but still get "Object doesn't support this property or method." Try it in IE8. It works fine in the snippet, but IE8 is another story.
– GTS Joe
Nov 14 '18 at 21:46
@Zze It is necessary to convert the result to a boolean. ~ is a bitwise not. It performs the not operation on each bit of the operand and then returns the result.
– hev1
Nov 14 '18 at 23:09
@Zze It is necessary to convert the result to a boolean. ~ is a bitwise not. It performs the not operation on each bit of the operand and then returns the result.
– hev1
Nov 14 '18 at 23:09
|
show 3 more comments
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53308396%2fhow-to-polyfill-array-prototype-includes-for-ie8%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
This should work if you only include the 'includes' polyfill. I suspect the problem lies somewhere in the order you have included all three
– Katie.Sun
Nov 14 '18 at 20:50
What about using cdn.polyfill.io/v2/polyfill.min.js from polyfill.io/v2/docs automatically detects what is missing in your browser =)
– Gabriel Lopez
Nov 14 '18 at 20:56
Why can't you use
indexOf? What's the reasoning?– Zze
Nov 14 '18 at 21:00
@Zze The main reason is I'm trying to learn how to use polyfills.
– GTS Joe
Nov 14 '18 at 21:03
@GTSJoe then polyfill
.includeswithindexOf? Polyfills are not a set implementation. There are many different implementations for polyfilling the same functions.– Zze
Nov 14 '18 at 21:23