propertyIsEnumerable.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. function write(v) { WScript.Echo(v + ""); }
  6. function foo() { this.y = 10; }
  7. var o = new Object();
  8. var f = new foo();
  9. var a = new Array();
  10. var s = new String("hello");
  11. var b = new Boolean(true);
  12. var n = new Number(10);
  13. var d = new Date();
  14. var r = new RegExp();
  15. var e = new Error();
  16. o.x = f.x = foo.x = a.x = s.x = b.x = n.x = d.x = r.x = e.x = 10;
  17. function doEval(str)
  18. {
  19. //write(str);
  20. write(str + " : " + eval(str));
  21. }
  22. // Check for standard properties on various built-in constructors
  23. function Test1() {
  24. var objs = [
  25. "Object", "Function", "Array", "String", "Boolean", "Number", "Math", "Date", "RegExp", "Error",
  26. "Object.prototype", "Function.prototype", "Array.prototype", "String.prototype", "Boolean.prototype",
  27. "Number.prototype", "Date.prototype", "RegExp.prototype", "Error.prototype",
  28. "o", "f", "foo", "foo.prototype", "a", "s", "b", "n", "d", "r", "e"
  29. ]
  30. var props = [
  31. "abs", "acos", "anchor", "apply", "Array", "asin", "atan", "atan2", "big", "bind",
  32. "blink", "bold", "Boolean", "call", "ceil", "charAt", "charCodeAt", "concat", "constructor", "cos",
  33. "Date", "decodeURI", "decodeURIComponent", "description", "E", "encodeURI", "encodeURIComponent", "escape", "Error", "eval", "EvalError",
  34. "every", "exec", "exp", "flags", "filter", "fixed", "floor", "fontcolor", "fontsize", "forEach",
  35. "fromCharCode", "Function", "getDate", "getDay", "getFullYear", "getHours", "getMilliseconds", "getMinutes", "getMonth", "getSeconds",
  36. "getTime", "getTimezoneOffset", "getUTCDate", "getUTCDay", "getUTCFullYear", "getUTCHours", "getUTCMilliseconds", "getUTCMinutes", "getUTCMonth",
  37. "getUTCSeconds", "getYear", "hasOwnProperty", "indexOf", "Infinity", "isFinite", "isNaN", "isPrototypeOf", "italics", "join", "lastIndexOf",
  38. "length", "link", "LN10", "LN2", "localeCompare", "log", "LOG10E", "LOG2E", "map", "Math",
  39. "max", "MAX_VALUE", "match", "message", "min", "MIN_VALUE", "NaN", "name", "Now", "Number",
  40. "number", "NEGATIVE_INFINITY", "Object", "parse", "parseFloat", "parseInt", "PI", "pop", "POSITIVE_INFINITY", "pow",
  41. "propertyIsEnumerable", "prototype", "push", "random", "RangeError", "reduce", "reduceRight", "ReferenceError", "replace", "reverse",
  42. "round", "RegExp", "search", "setDate", "setFullYear", "setHours", "setMilliseconds", "setMinutes", "setMonth", "setSeconds",
  43. "setTime", "setUTCDate", "setUTCFullYear", "setUTCHours", "setUTCMilliseconds", "setUTCMinutes", "setUTCMonth", "setUTCSeconds", "setYear", "shift",
  44. "sin", "slice", "some", "sort", "source", "splice", "split", "sqrt", "SQRT1_2", "SQRT2",
  45. "strike", "String", "sub", "substring", "substr", "sup", "SyntaxError", "tan", "test", "toDateString",
  46. "toExponential", "toFixed", "toISOString", "toJSON", "toLocaleDateString", "toLocaleLowerCase", "toLocaleString", "toLocaleTimeString",
  47. "toLocaleUpperCase", "toLowerCase", "toPrecision", "toString", "toTimeString", "toUpperCase", "toUTCString", "trim", "TypeError", "undefined",
  48. "unescape", "unshift", "URIError", "UTC", "valueOf", "enumerable", "configurable", "writable", "value", "get", "set", "defineProperty",
  49. "defineProperties", "toGMTString", "compile", "global", "lastIndex", "multiline", "ignoreCase", "index", "input",
  50. "lastMatch", "lastParen", "leftContext", "rightContext",
  51. "x", "y"
  52. ];
  53. for (var i=0; i<objs.length; i++)
  54. {
  55. for (var j=0; j< props.length; j++)
  56. {
  57. doEval(objs[i] + ".propertyIsEnumerable(\"" + props[j] + "\")");
  58. }
  59. }
  60. }
  61. function Test2() {
  62. function base() {
  63. this.x = "base.x";
  64. this.y = "base.y";
  65. }
  66. function derived() {
  67. this.y = "derived.y";
  68. this.z = "derived.z";
  69. }
  70. derived.prototype = new base();
  71. var d = new derived();
  72. write("Test2 d.propertyIsEnumerable(x): " + d.propertyIsEnumerable("x"));
  73. write("Test2 d.propertyIsEnumerable(y): " + d.propertyIsEnumerable("y"));
  74. write("Test2 d.propertyIsEnumerable(z): " + d.propertyIsEnumerable("z"));
  75. write("Test2 d.hasOwnProperty(x): " + d.hasOwnProperty("x"));
  76. write("Test2 d.hasOwnProperty(y): " + d.hasOwnProperty("y"));
  77. write("Test2 d.hasOwnProperty(z): " + d.hasOwnProperty("z"));
  78. }
  79. function Test3() {
  80. try {
  81. write(Object.prototype.propertyIsEnumerable.call(undefined, "length"));
  82. } catch (e) {
  83. write("Exception: " + e + " " + e.message);
  84. }
  85. try {
  86. write(Object.prototype.propertyIsEnumerable.call(null, "length"));
  87. } catch (e) {
  88. write("Exception: " + e + " " + e.message);
  89. }
  90. }
  91. var re = new RegExp("d(b+)(d)", "ig");
  92. function TestRegex()
  93. {
  94. var propso = [
  95. "lastIndex", "source", "global", "ignoreCase", "multiline", "options"];
  96. var props = [
  97. "input","$_","index","lastIndex","lastMatch",'$&',"lastParen",'$+','$`',"rightContext",
  98. "$'","$2","$3","$4","$5","$6","$7","$8","$9"];
  99. for (var j=0; j< props.length; j++)
  100. {
  101. doEval("re" + ".propertyIsEnumerable(\"" + propso[j] + "\")");
  102. }
  103. for (var j=0; j< props.length; j++)
  104. {
  105. doEval("RegExp" + ".propertyIsEnumerable(\"" + props[j] + "\")");
  106. }
  107. }
  108. Test1();
  109. Test2();
  110. Test3();
  111. TestRegex();