es5.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. var isES5 = (function(){
  2. "use strict";
  3. return this === undefined;
  4. })();
  5. if (isES5) {
  6. module.exports = {
  7. freeze: Object.freeze,
  8. defineProperty: Object.defineProperty,
  9. getDescriptor: Object.getOwnPropertyDescriptor,
  10. keys: Object.keys,
  11. names: Object.getOwnPropertyNames,
  12. getPrototypeOf: Object.getPrototypeOf,
  13. isArray: Array.isArray,
  14. isES5: isES5,
  15. propertyIsWritable: function(obj, prop) {
  16. var descriptor = Object.getOwnPropertyDescriptor(obj, prop);
  17. return !!(!descriptor || descriptor.writable || descriptor.set);
  18. }
  19. };
  20. } else {
  21. var has = {}.hasOwnProperty;
  22. var str = {}.toString;
  23. var proto = {}.constructor.prototype;
  24. var ObjectKeys = function (o) {
  25. var ret = [];
  26. for (var key in o) {
  27. if (has.call(o, key)) {
  28. ret.push(key);
  29. }
  30. }
  31. return ret;
  32. };
  33. var ObjectGetDescriptor = function(o, key) {
  34. return {value: o[key]};
  35. };
  36. var ObjectDefineProperty = function (o, key, desc) {
  37. o[key] = desc.value;
  38. return o;
  39. };
  40. var ObjectFreeze = function (obj) {
  41. return obj;
  42. };
  43. var ObjectGetPrototypeOf = function (obj) {
  44. try {
  45. return Object(obj).constructor.prototype;
  46. }
  47. catch (e) {
  48. return proto;
  49. }
  50. };
  51. var ArrayIsArray = function (obj) {
  52. try {
  53. return str.call(obj) === "[object Array]";
  54. }
  55. catch(e) {
  56. return false;
  57. }
  58. };
  59. module.exports = {
  60. isArray: ArrayIsArray,
  61. keys: ObjectKeys,
  62. names: ObjectKeys,
  63. defineProperty: ObjectDefineProperty,
  64. getDescriptor: ObjectGetDescriptor,
  65. freeze: ObjectFreeze,
  66. getPrototypeOf: ObjectGetPrototypeOf,
  67. isES5: isES5,
  68. propertyIsWritable: function() {
  69. return true;
  70. }
  71. };
  72. }