在原生不支持的旧环境中添加兼容的Object.keys实现方法

发布时间:2020-09-10 17:14:00 作者:不要熬夜
来源:脚本之家 阅读:211

如下所示:

if (!Object.keys) {
 Object.keys = (function () {
  var hasOwnProperty = Object.prototype.hasOwnProperty, //原型上的方法,只取自身有的属性;
    hasDontEnumBug = !({toString: null}).propertyIsEnumerable('toString'), //ie6一下,!之后的内容为false;
    dontEnums = [
     'toString',
     'toLocaleString',
     'valueOf',
     'hasOwnProperty',
     'isPrototypeOf',
     'propertyIsEnumerable',
     'constructor'
    ],
    dontEnumsLength = dontEnums.length;

  return function (obj) {
   if (typeof obj !== 'object' && typeof obj !== 'function' || obj === null) throw new TypeError('Object.keys called on non-object');

   var result = [];

   for (var prop in obj) {
    if (hasOwnProperty.call(obj, prop)) result.push(prop);
   }

   if (hasDontEnumBug) {
    for (var i=0; i < dontEnumsLength; i++) {
     if (hasOwnProperty.call(obj, dontEnums[i])) result.push(dontEnums[i]);
    }
   }
   return result;
  }
 })()
};

以上这篇在原生不支持的旧环境中添加兼容的Object.keys实现方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持亿速云。

推荐阅读:
  1. JavaScript中Object.keys方法获取对象属性的方法
  2. python添加环境变量的方法

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

添加 兼容 object.keys

上一篇:你还在 Select * 吗?

下一篇:mybatis 加载配置文件的方法(两种方式)

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》