Object.html 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>The source code</title>
  6. <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
  7. <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
  8. <style type="text/css">
  9. .highlight { display: block; background-color: #ddd; }
  10. </style>
  11. <script type="text/javascript">
  12. function highlight() {
  13. document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
  14. }
  15. </script>
  16. </head>
  17. <body onload="prettyPrint(); highlight();">
  18. <pre class="prettyprint lang-js"><span id='Object'>/**
  19. </span> * @class Object
  20. *
  21. * Creates an object wrapper.
  22. *
  23. * The Object constructor creates an object wrapper for the given value. If the value is null or
  24. * undefined, it will create and return an empty object, otherwise, it will return an object of a type
  25. * that corresponds to the given value.
  26. *
  27. * When called in a non-constructor context, Object behaves identically.
  28. *
  29. * # Using Object given undefined and null types
  30. *
  31. * The following examples store an empty Object object in o:
  32. * var o = new Object();
  33. *
  34. * var o = new Object(undefined);
  35. *
  36. * var o = new Object(null);
  37. *
  38. * # Using Object to create Boolean objects
  39. *
  40. * The following examples store Boolean objects in o:
  41. *
  42. * // equivalent to o = new Boolean(true);
  43. * var o = new Object(true);
  44. *
  45. * // equivalent to o = new Boolean(false);
  46. * var o = new Object(Boolean());
  47. *
  48. * &lt;div class=&quot;notice&quot;&gt;
  49. * Documentation for this class comes from &lt;a href=&quot;https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object&quot;&gt;MDN&lt;/a&gt;
  50. * and is available under &lt;a href=&quot;http://creativecommons.org/licenses/by-sa/2.0/&quot;&gt;Creative Commons: Attribution-Sharealike license&lt;/a&gt;.
  51. * &lt;/div&gt;
  52. */
  53. <span id='Object-method-constructor'>/**
  54. </span> * @method constructor
  55. * Creates new Object.
  56. * @param {Object} [value] The value to wrap.
  57. */
  58. //Properties
  59. <span id='Object-property-prototype'>/**
  60. </span> * @property prototype
  61. * Allows the addition of properties to all objects of type Object.
  62. */
  63. //Methods
  64. <span id='Object-method-hasOwnProperty'>/**
  65. </span> * @method hasOwnProperty
  66. * Returns a boolean indicating whether an object contains the specified property as a direct property
  67. * of that object and not inherited through the prototype chain.
  68. *
  69. * Every object descended from `Object` inherits the `hasOwnProperty` method. This method can be used
  70. * to determine whether an object has the specified property as a direct property of that object;
  71. * unlike the `in` operator, this method does not check down the object's prototype chain.
  72. *
  73. * The following example determines whether the o object contains a property named prop:
  74. *
  75. * o = new Object();
  76. * o.prop = 'exists';
  77. *
  78. * function changeO() {
  79. * o.newprop = o.prop;
  80. * delete o.prop;
  81. * }
  82. *
  83. * o.hasOwnProperty('prop'); //returns true
  84. * changeO();
  85. * o.hasOwnProperty('prop'); //returns false
  86. *
  87. * The following example differentiates between direct properties and properties inherited through the
  88. * prototype chain:
  89. *
  90. * o = new Object();
  91. * o.prop = 'exists';
  92. * o.hasOwnProperty('prop'); // returns true
  93. * o.hasOwnProperty('toString'); // returns false
  94. * o.hasOwnProperty('hasOwnProperty'); // returns false
  95. *
  96. * The following example shows how to iterate over the properties of an object without executing on
  97. * inherit properties.
  98. *
  99. * var buz = {
  100. * fog: 'stack'
  101. * };
  102. *
  103. * for (var name in buz) {
  104. * if (buz.hasOwnProperty(name)) {
  105. * alert(&quot;this is fog (&quot; + name + &quot;) for sure. Value: &quot; + buz[name]);
  106. * }
  107. * else {
  108. * alert(name); // toString or something else
  109. * }
  110. * }
  111. *
  112. * @param {String} prop The name of the property to test.
  113. * @return {Boolean} Returns true if object contains specified property; else
  114. * returns false.
  115. */
  116. <span id='Object-method-isPrototypeOf'>/**
  117. </span> * @method isPrototypeOf
  118. * Returns a boolean indication whether the specified object is in the prototype chain of the object
  119. * this method is called upon.
  120. *
  121. * `isPrototypeOf` allows you to check whether or not an object exists within another object's
  122. * prototype chain.
  123. *
  124. * For example, consider the following prototype chain:
  125. *
  126. * function Fee() {
  127. * // . . .
  128. * }
  129. *
  130. * function Fi() {
  131. * // . . .
  132. * }
  133. * Fi.prototype = new Fee();
  134. *
  135. * function Fo() {
  136. * // . . .
  137. * }
  138. * Fo.prototype = new Fi();
  139. *
  140. * function Fum() {
  141. * // . . .
  142. * }
  143. * Fum.prototype = new Fo();
  144. *
  145. * Later on down the road, if you instantiate `Fum` and need to check if `Fi`'s prototype exists
  146. * within the `Fum` prototype chain, you could do this:
  147. *
  148. * var fum = new Fum();
  149. * . . .
  150. *
  151. * if (Fi.prototype.isPrototypeOf(fum)) {
  152. * // do something safe
  153. * }
  154. *
  155. * This, along with the `instanceof` operator particularly comes in handy if you have code that can
  156. * only function when dealing with objects descended from a specific prototype chain, e.g., to
  157. * guarantee that certain methods or properties will be present on that object.
  158. *
  159. * @param {Object} prototype an object to be tested against each link in the prototype chain of the
  160. * *object* argument
  161. * @param {Object} object the object whose prototype chain will be searched
  162. * @return {Boolean} Returns true if object is a prototype and false if not.
  163. */
  164. <span id='Object-method-propertyIsEnumerable'>/**
  165. </span> * @method propertyIsEnumerable
  166. * Returns a boolean indicating if the internal ECMAScript DontEnum attribute is set.
  167. *
  168. * Every object has a `propertyIsEnumerable` method. This method can determine whether the specified
  169. * property in an object can be enumerated by a `for...in` loop, with the exception of properties
  170. * inherited through the prototype chain. If the object does not have the specified property, this
  171. * method returns false.
  172. *
  173. * The following example shows the use of `propertyIsEnumerable` on objects and arrays:
  174. *
  175. * var o = {};
  176. * var a = [];
  177. * o.prop = 'is enumerable';
  178. * a[0] = 'is enumerable';
  179. *
  180. * o.propertyIsEnumerable('prop'); // returns true
  181. * a.propertyIsEnumerable(0); // returns true
  182. *
  183. * The following example demonstrates the enumerability of user-defined versus built-in properties:
  184. *
  185. * var a = ['is enumerable'];
  186. *
  187. * a.propertyIsEnumerable(0); // returns true
  188. * a.propertyIsEnumerable('length'); // returns false
  189. *
  190. * Math.propertyIsEnumerable('random'); // returns false
  191. * this.propertyIsEnumerable('Math'); // returns false
  192. *
  193. * Direct versus inherited properties
  194. *
  195. * var a = [];
  196. * a.propertyIsEnumerable('constructor'); // returns false
  197. *
  198. * function firstConstructor()
  199. * {
  200. * this.property = 'is not enumerable';
  201. * }
  202. * firstConstructor.prototype.firstMethod = function () {};
  203. *
  204. * function secondConstructor()
  205. * {
  206. * this.method = function method() { return 'is enumerable'; };
  207. * }
  208. *
  209. * secondConstructor.prototype = new firstConstructor;
  210. * secondConstructor.prototype.constructor = secondConstructor;
  211. *
  212. * var o = new secondConstructor();
  213. * o.arbitraryProperty = 'is enumerable';
  214. *
  215. * o.propertyIsEnumerable('arbitraryProperty'); // returns true
  216. * o.propertyIsEnumerable('method'); // returns true
  217. * o.propertyIsEnumerable('property'); // returns false
  218. *
  219. * o.property = 'is enumerable';
  220. *
  221. * o.propertyIsEnumerable('property'); // returns true
  222. *
  223. * // These return false as they are on the prototype which
  224. * // propertyIsEnumerable does not consider (even though the last two
  225. * // are iteratable with for-in)
  226. * o.propertyIsEnumerable('prototype'); // returns false (as of JS 1.8.1/FF3.6)
  227. * o.propertyIsEnumerable('constructor'); // returns false
  228. * o.propertyIsEnumerable('firstMethod'); // returns false
  229. *
  230. * @param {String} prop The name of the property to test.
  231. * @return {Boolean} If the object does not have the specified property, this
  232. * method returns false.
  233. */
  234. <span id='Object-method-toLocaleString'>/**
  235. </span> * @method toLocaleString
  236. * Returns a string representing the object. This method is meant to be overridden by derived objects
  237. * for locale-specific purposes.
  238. *
  239. * `Object`'s `toLocaleString` returns the result of calling `toString`.
  240. *
  241. * This function is provided to give objects a generic `toLocaleString` method, even though not all
  242. * may use it. Currently, only `Array`, `Number`, and `Date` override `toLocaleString`.
  243. *
  244. * @return {String} Object represented as a string.
  245. */
  246. <span id='Object-method-toString'>/**
  247. </span> * @method toString
  248. * Returns a string representation of the object.
  249. *
  250. * Every object has a `toString()` method that is automatically called when the object is to be
  251. * represented as a text value or when an object is referred to in a manner in which a string is
  252. * expected. By default, the `toString()` method is inherited by every object descended from `Object`.
  253. * If this method is not overridden in a custom object, `toString()` returns &quot;[object type]&quot;, where
  254. * `type` is the object type. The following code illustrates this:
  255. *
  256. * var o = new Object();
  257. * o.toString(); // returns [object Object]
  258. *
  259. * You can create a function to be called in place of the default `toString()` method. The
  260. * `toString()` method takes no arguments and should return a string. The `toString()` method you
  261. * create can be any value you want, but it will be most useful if it carries information about the
  262. * object.
  263. *
  264. * The following code defines the `Dog` object type and creates `theDog`, an object of type `Dog`:
  265. *
  266. * function Dog(name,breed,color,sex) {
  267. * this.name=name;
  268. * this.breed=breed;
  269. * this.color=color;
  270. * this.sex=sex;
  271. * }
  272. *
  273. * theDog = new Dog(&quot;Gabby&quot;,&quot;Lab&quot;,&quot;chocolate&quot;,&quot;female&quot;);
  274. *
  275. * If you call the `toString()` method on this custom object, it returns the default value inherited
  276. * from `Object`:
  277. *
  278. * theDog.toString(); //returns [object Object]
  279. *
  280. * The following code creates and assigns `dogToString()` to override the default `toString()` method.
  281. * This function generates a string containing the name, breed, color, and sex of the object, in the
  282. * form `&quot;property = value;&quot;`.
  283. *
  284. * Dog.prototype.toString = function dogToString() {
  285. * var ret = &quot;Dog &quot; + this.name + &quot; is a &quot; + this.sex + &quot; &quot; + this.color + &quot; &quot; + this.breed;
  286. * return ret;
  287. * }
  288. *
  289. * With the preceding code in place, any time theDog is used in a string context, JavaScript
  290. * automatically calls the `dogToString()` function, which returns the following string:
  291. *
  292. * Dog Gabby is a female chocolate Lab
  293. *
  294. * `toString()` can be used with every object and allows you to get its class. To use the
  295. * `Object.prototype.toString()` with every object, you need to call `Function.prototype.call()` or
  296. * `Function.prototype.apply()` on it, passing the object you want to inspect as the first parameter
  297. * called `thisArg`.
  298. *
  299. * var toString = Object.prototype.toString;
  300. *
  301. * toString.call(new Date); // [object Date]
  302. * toString.call(new String); // [object String]
  303. * toString.call(Math); // [object Math]
  304. *
  305. * @return {String} Object represented as a string.
  306. */
  307. <span id='Object-method-valueOf'>/**
  308. </span> * @method valueOf
  309. * Returns the primitive value of the specified object.
  310. *
  311. * JavaScript calls the `valueOf` method to convert an object to a primitive value. You rarely need to
  312. * invoke the `valueOf` method yourself; JavaScript automatically invokes it when encountering an
  313. * object where a primitive value is expected.
  314. *
  315. * By default, the `valueOf` method is inherited by every object descended from `Object`. Every built-
  316. * in core object overrides this method to return an appropriate value. If an object has no primitive
  317. * value, `valueOf` returns the object itself, which is displayed as:
  318. *
  319. * [object Object]
  320. *
  321. * You can use `valueOf` within your own code to convert a built-in object into a primitive value.
  322. * When you create a custom object, you can override `Object.valueOf` to call a custom method instead
  323. * of the default `Object` method.
  324. *
  325. * You can create a function to be called in place of the default `valueOf` method. Your function must
  326. * take no arguments.
  327. *
  328. * Suppose you have an object type `myNumberType` and you want to create a `valueOf` method for it.
  329. * The following code assigns a user-defined function to the object's valueOf method:
  330. *
  331. * myNumberType.prototype.valueOf = new Function(functionText)
  332. *
  333. * With the preceding code in place, any time an object of type `myNumberType` is used in a context
  334. * where it is to be represented as a primitive value, JavaScript automatically calls the function
  335. * defined in the preceding code.
  336. *
  337. * An object's `valueOf` method is usually invoked by JavaScript, but you can invoke it yourself as
  338. * follows:
  339. *
  340. * myNumber.valueOf()
  341. *
  342. * Note: Objects in string contexts convert via the `toString` method, which is different from
  343. * `String` objects converting to string primitives using `valueOf`. All objects have a string
  344. * conversion, if only `&quot;[object type]&quot;`. But many objects do not convert to number, boolean, or
  345. * function.
  346. *
  347. * @return {Object} Returns value of the object or the object itself.
  348. */
  349. //Properties
  350. <span id='Object-property-constructor'>/**
  351. </span> * @property constructor
  352. * Specifies the function that creates an object's prototype.
  353. *
  354. * Returns a reference to the Object function that created the instance's prototype. Note that the
  355. * value of this property is a reference to the function itself, not a string containing the
  356. * function's name, but it isn't read only (except for primitive Boolean, Number or String values: 1,
  357. * true, &quot;read-only&quot;).
  358. *
  359. * All objects inherit a `constructor` property from their `prototype`:
  360. *
  361. * o = new Object // or o = {} in JavaScript 1.2
  362. * o.constructor == Object
  363. * a = new Array // or a = [] in JavaScript 1.2
  364. * a.constructor == Array
  365. * n = new Number(3)
  366. * n.constructor == Number
  367. *
  368. * Even though you cannot construct most HTML objects, you can do comparisons. For example,
  369. *
  370. * document.constructor == Document
  371. * document.form3.constructor == Form
  372. *
  373. * The following example creates a prototype, `Tree`, and an object of that type, theTree. The example then displays the `constructor` property for the object `theTree`.
  374. *
  375. * function Tree(name) {
  376. * this.name = name;
  377. * }
  378. * theTree = new Tree(&quot;Redwood&quot;);
  379. * console.log(&quot;theTree.constructor is &quot; + theTree.constructor);
  380. *
  381. * This example displays the following output:
  382. *
  383. * theTree.constructor is function Tree(name) {
  384. * this.name = name;
  385. * }
  386. *
  387. * The following example shows how to modify constructor value of generic objects. Only true, 1 and
  388. * &quot;test&quot; variable constructors will not be changed. This example explains that is not always so safe
  389. * to believe in constructor function.
  390. *
  391. * function Type(){};
  392. * var types = [
  393. * new Array, [],
  394. * new Boolean, true,
  395. * new Date,
  396. * new Error,
  397. * new Function, function(){},
  398. * Math,
  399. * new Number, 1,
  400. * new Object, {},
  401. * new RegExp, /(?:)/,
  402. * new String, &quot;test&quot;
  403. * ];
  404. * for(var i = 0; i &lt; types.length; i++){
  405. * types[i].constructor = Type;
  406. * types[i] = [types[i].constructor, types[i] instanceof Type, types[i].toString()];
  407. * };
  408. * alert(types.join(&quot;\n&quot;));
  409. */</pre>
  410. </body>
  411. </html>