Skip to content

Commit

Permalink
Avoid IE8’s broken Object.defineProperty
Browse files Browse the repository at this point in the history
IE 8 only supports `Object.defineProperty` on DOM elements, so detect that and fall back.
  • Loading branch information
mathiasbynens committed Jan 8, 2014
1 parent 614cdb7 commit 1cdc47f
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions startswith.js
Expand Up @@ -2,6 +2,15 @@
if (!String.prototype.startsWith) {
(function() {
'use strict'; // needed to support `apply`/`call` with `undefined`/`null`
var defineProperty = (function() {
// IE 8 only supports `Object.defineProperty` on DOM elements
try {
var object = {};
var $defineProperty = Object.defineProperty;
var result = $defineProperty(object, object, object) && $defineProperty;
} catch(error) {}
return result;
}());
var toString = {}.toString;
var startsWith = function(search) {
if (this == null) {
Expand Down Expand Up @@ -33,8 +42,8 @@ if (!String.prototype.startsWith) {
}
return true;
};
if (Object.defineProperty) {
Object.defineProperty(String.prototype, 'startsWith', {
if (defineProperty) {
defineProperty(String.prototype, 'startsWith', {
'value': startsWith,
'configurable': true,
'writable': true
Expand Down

0 comments on commit 1cdc47f

Please sign in to comment.