Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

String.prototype.startsWith()

该新特性属于 ECMAScript 2015(ES6)规范,在使用时请注意浏览器兼容性。

概述

startsWith()方法用来判断当前字符串是否是以另外一个给定的子字符串“开头”的,根据判断结果返回 truefalse

语法

str.startsWith(searchString [, position]);

参数

searchString
要搜索的子字符串。
position
str 中搜索 searchString 的开始位置,默认值为 0,也就是真正的字符串开头处。

示例

var str = "To be, or not to be, that is the question.";

alert(str.startsWith("To be"));         // true
alert(str.startsWith("not to be"));     // false
alert(str.startsWith("not to be", 10)); // true

Polyfill

/*! https://mths.be/startswith v0.2.0 by @mathias */
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) {
        throw TypeError();
      }
      var string = String(this);
      if (search && toString.call(search) == '[object RegExp]') {
        throw TypeError();
      }
      var stringLength = string.length;
      var searchString = String(search);
      var searchLength = searchString.length;
      var position = arguments.length > 1 ? arguments[1] : undefined;
      // `ToInteger`
      var pos = position ? Number(position) : 0;
      if (pos != pos) { // better `isNaN`
        pos = 0;
      }
      var start = Math.min(Math.max(pos, 0), stringLength);
      // Avoid the `indexOf` call if no match is possible
      if (searchLength + start > stringLength) {
        return false;
      }
      var index = -1;
      while (++index < searchLength) {
        if (string.charCodeAt(start + index) != searchString.charCodeAt(index)) {
          return false;
        }
      }
      return true;
    };
    if (defineProperty) {
      defineProperty(String.prototype, 'startsWith', {
        'value': startsWith,
        'configurable': true,
        'writable': true
      });
    } else {
      String.prototype.startsWith = startsWith;
    }
  }());
}

浏览器兼容性

Feature Chrome Firefox (Gecko) Edge Internet Explorer Opera Safari
Basic support 41 17 (17) (Yes) 未实现 28 9
Feature Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support 未实现 36 17.0 (17) 未实现 未实现 未实现

 

Please note that although the MSDN documentation for this method (https://msdn.microsoft.com/en-us/library/mt146831(v=vs.94).aspx) clearly indicates that it is not supported in Internet Explorer, the method does seem to work, as far back as Internet Explorer 8.

相关链接

文档标签和贡献者

 此页面的贡献者: Meteormatt, ziyunfei, teoli, zhangyaochun1987
 最后编辑者: Meteormatt,