当前位置: 首页 > 工具软件 > ES6-Shim > 使用案例 >

摘取的es5-shim/es6-shim

邓高韵
2023-12-01

摘取的es5-shim/es6-shim

Object.create

var create = Object.create || function (prototype, properties) {
    var Prototype = function Prototype() {};
    Prototype.prototype = prototype;
    var object = new Prototype();
    if (typeof properties !== 'undefined') {
      keys(properties).forEach(function (key) {
        Value.defineByDescriptor(object, key, properties[key]);
      });
    }
    return object;
};

isObject

TypeIsObject: function (x) {
      if (x === void 0 || x === null || x === true || x === false) {
        return false;
      }
      return typeof x === 'function' || typeof x === 'object' || x === domAll;
 }

isArray

var isArray = Array.isArray || function isArray(obj) {
        return Object.prototype.toString.call(obj) === '[object Array]';
};

类型转换:

var ES = {
    // http://www.ecma-international.org/ecma-262/6.0/#sec-call
    Call: function Call(F, V) {
      var args = arguments.length > 2 ? arguments[2] : [];
      if (!ES.IsCallable(F)) {
        throw new TypeError(F + ' is not a function');
      }
      return _apply(F, V, args);
    },
    RequireObjectCoercible: function (x, optMessage) {
      if (isNullOrUndefined(x)) {
        throw new TypeError(optMessage || 'Cannot call method on ' + x);
      }
      return x;
 }
ToObject: function (o, optMessage) {
      return Object(ES.RequireObjectCoercible(o, optMessage));
}
ToNumber: function (value) {
      if (_toString(value) === '[object Symbol]') {
        throw new TypeError('Cannot convert a Symbol value to a number');
      }
      return +value;
}
ToInt32: function (x) {
      return ES.ToNumber(x) >> 0;
 },
ToUint32: function (x) {
      return ES.ToNumber(x) >>> 0;
},
ToInteger: function (value) {
      var number = ES.ToNumber(value);
      if (numberIsNaN(number)) { return 0; }
      if (number === 0 || !numberIsFinite(number)) { return number; }
      return (number > 0 ? 1 : -1) * _floor(_abs(number));
}

push

 var pushShim = function push(item) {
        var O = ES.ToObject(this);
        var n = ES.ToUint32(O.length);
        var i = 0;
        while (i < arguments.length) {
            O[n + i] = arguments[i];
            i += 1;
        }
        O.length = n + i;
        return n + i;
    };
push: function push(item) {
     if (isArray(this)) {
           return array_push.apply(this, arguments);
     }
     return pushShim.apply(this, arguments);
 }

Date.now()

 if (!Date.now) {
        Date.now = function now() {
            return new Date().getTime();
        };
}

或者也可以是:

 if (!Date.now) {
        Date.now = function now() {
            return +new Date();
        };
    }

具体内容可去github查询,地址为

es5-shim:https://github.com/SanchunPeng/es5-shim
es6-shim:https://github.com/SanchunPeng/es6-shim

 类似资料: