String prototypes polyfills on Object.defineProperty

This commit is contained in:
Ilya Goncharov
2020-06-01 18:47:31 +03:00
committed by Ilya Goncharov
parent 9431fc4693
commit a476d1dbc4
+8 -4
View File
@@ -4,13 +4,16 @@
*/ */
if (typeof String.prototype.startsWith === "undefined") { if (typeof String.prototype.startsWith === "undefined") {
String.prototype.startsWith = function(searchString, position) { Object.defineProperty(String.prototype, "startsWith", {
value: function (searchString, position) {
position = position || 0; position = position || 0;
return this.lastIndexOf(searchString, position) === position; return this.lastIndexOf(searchString, position) === position;
}; }
});
} }
if (typeof String.prototype.endsWith === "undefined") { if (typeof String.prototype.endsWith === "undefined") {
String.prototype.endsWith = function(searchString, position) { Object.defineProperty(String.prototype, "endsWith", {
value: function (searchString, position) {
var subjectString = this.toString(); var subjectString = this.toString();
if (position === undefined || position > subjectString.length) { if (position === undefined || position > subjectString.length) {
position = subjectString.length; position = subjectString.length;
@@ -18,7 +21,8 @@ if (typeof String.prototype.endsWith === "undefined") {
position -= searchString.length; position -= searchString.length;
var lastIndex = subjectString.indexOf(searchString, position); var lastIndex = subjectString.indexOf(searchString, position);
return lastIndex !== -1 && lastIndex === position; return lastIndex !== -1 && lastIndex === position;
}; }
});
} }
// ES6 Math polyfills // ES6 Math polyfills
if (typeof Math.sign === "undefined") { if (typeof Math.sign === "undefined") {