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; }
11 <script type="text/javascript">
12 function highlight() {
13 document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
17 <body onload="prettyPrint(); highlight();">
18 <pre class="prettyprint lang-js"><span id='Object'>/**
19 </span> * @class Object
21 * Creates an object wrapper.
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.
27 * When called in a non-constructor context, Object behaves identically.
29 * # Using Object given undefined and null types
31 * The following examples store an empty Object object in o:
32 * var o = new Object();
34 * var o = new Object(undefined);
36 * var o = new Object(null);
38 * # Using Object to create Boolean objects
40 * The following examples store Boolean objects in o:
42 * // equivalent to o = new Boolean(true);
43 * var o = new Object(true);
45 * // equivalent to o = new Boolean(false);
46 * var o = new Object(Boolean());
48 * <div class="notice">
49 * Documentation for this class comes from <a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object">MDN</a>
50 * and is available under <a href="http://creativecommons.org/licenses/by-sa/2.0/">Creative Commons: Attribution-Sharealike license</a>.
54 <span id='Object-method-constructor'>/**
55 </span> * @method constructor
57 * @param {Object} [value] The value to wrap.
62 <span id='Object-property-prototype'>/**
63 </span> * @property prototype
64 * Allows the addition of properties to all objects of type Object.
69 <span id='Object-method-hasOwnProperty'>/**
70 </span> * @method hasOwnProperty
71 * Returns a boolean indicating whether an object contains the specified property as a direct property
72 * of that object and not inherited through the prototype chain.
74 * Every object descended from `Object` inherits the `hasOwnProperty` method. This method can be used
75 * to determine whether an object has the specified property as a direct property of that object;
76 * unlike the `in` operator, this method does not check down the object's prototype chain.
78 * The following example determines whether the o object contains a property named prop:
83 * function changeO() {
88 * o.hasOwnProperty('prop'); //returns true
90 * o.hasOwnProperty('prop'); //returns false
92 * The following example differentiates between direct properties and properties inherited through the
97 * o.hasOwnProperty('prop'); // returns true
98 * o.hasOwnProperty('toString'); // returns false
99 * o.hasOwnProperty('hasOwnProperty'); // returns false
101 * The following example shows how to iterate over the properties of an object without executing on
102 * inherit properties.
108 * for (var name in buz) {
109 * if (buz.hasOwnProperty(name)) {
110 * alert("this is fog (" + name + ") for sure. Value: " + buz[name]);
113 * alert(name); // toString or something else
117 * @param {String} prop The name of the property to test.
118 * @return {Boolean} Returns true if object contains specified property; else
122 <span id='Object-method-isPrototypeOf'>/**
123 </span> * @method isPrototypeOf
124 * Returns a boolean indication whether the specified object is in the prototype chain of the object
125 * this method is called upon.
127 * `isPrototypeOf` allows you to check whether or not an object exists within another object's
130 * For example, consider the following prototype chain:
139 * Fi.prototype = new Fee();
144 * Fo.prototype = new Fi();
149 * Fum.prototype = new Fo();
151 * Later on down the road, if you instantiate `Fum` and need to check if `Fi`'s prototype exists
152 * within the `Fum` prototype chain, you could do this:
154 * var fum = new Fum();
157 * if (Fi.prototype.isPrototypeOf(fum)) {
158 * // do something safe
161 * This, along with the `instanceof` operator particularly comes in handy if you have code that can
162 * only function when dealing with objects descended from a specific prototype chain, e.g., to
163 * guarantee that certain methods or properties will be present on that object.
165 * @param {Object} prototype an object to be tested against each link in the prototype chain of the
167 * @param {Object} object the object whose prototype chain will be searched
168 * @return {Boolean} Returns true if object is a prototype and false if not.
171 <span id='Object-method-propertyIsEnumerable'>/**
172 </span> * @method propertyIsEnumerable
173 * Returns a boolean indicating if the internal ECMAScript DontEnum attribute is set.
175 * Every object has a `propertyIsEnumerable` method. This method can determine whether the specified
176 * property in an object can be enumerated by a `for...in` loop, with the exception of properties
177 * inherited through the prototype chain. If the object does not have the specified property, this
178 * method returns false.
180 * The following example shows the use of `propertyIsEnumerable` on objects and arrays:
184 * o.prop = 'is enumerable';
185 * a[0] = 'is enumerable';
187 * o.propertyIsEnumerable('prop'); // returns true
188 * a.propertyIsEnumerable(0); // returns true
190 * The following example demonstrates the enumerability of user-defined versus built-in properties:
192 * var a = ['is enumerable'];
194 * a.propertyIsEnumerable(0); // returns true
195 * a.propertyIsEnumerable('length'); // returns false
197 * Math.propertyIsEnumerable('random'); // returns false
198 * this.propertyIsEnumerable('Math'); // returns false
200 * Direct versus inherited properties
203 * a.propertyIsEnumerable('constructor'); // returns false
205 * function firstConstructor()
207 * this.property = 'is not enumerable';
209 * firstConstructor.prototype.firstMethod = function () {};
211 * function secondConstructor()
213 * this.method = function method() { return 'is enumerable'; };
216 * secondConstructor.prototype = new firstConstructor;
217 * secondConstructor.prototype.constructor = secondConstructor;
219 * var o = new secondConstructor();
220 * o.arbitraryProperty = 'is enumerable';
222 * o.propertyIsEnumerable('arbitraryProperty'); // returns true
223 * o.propertyIsEnumerable('method'); // returns true
224 * o.propertyIsEnumerable('property'); // returns false
226 * o.property = 'is enumerable';
228 * o.propertyIsEnumerable('property'); // returns true
230 * // These return false as they are on the prototype which
231 * // propertyIsEnumerable does not consider (even though the last two
232 * // are iteratable with for-in)
233 * o.propertyIsEnumerable('prototype'); // returns false (as of JS 1.8.1/FF3.6)
234 * o.propertyIsEnumerable('constructor'); // returns false
235 * o.propertyIsEnumerable('firstMethod'); // returns false
237 * @param {String} prop The name of the property to test.
238 * @return {Boolean} If the object does not have the specified property, this
239 * method returns false.
242 <span id='Object-method-toLocaleString'>/**
243 </span> * @method toLocaleString
244 * Returns a string representing the object. This method is meant to be overridden by derived objects
245 * for locale-specific purposes.
247 * `Object`'s `toLocaleString` returns the result of calling `toString`.
249 * This function is provided to give objects a generic `toLocaleString` method, even though not all
250 * may use it. Currently, only `Array`, `Number`, and `Date` override `toLocaleString`.
252 * @return {String} Object represented as a string.
255 <span id='Object-method-toString'>/**
256 </span> * @method toString
257 * Returns a string representation of the object.
259 * Every object has a `toString()` method that is automatically called when the object is to be
260 * represented as a text value or when an object is referred to in a manner in which a string is
261 * expected. By default, the `toString()` method is inherited by every object descended from `Object`.
262 * If this method is not overridden in a custom object, `toString()` returns "[object type]", where
263 * `type` is the object type. The following code illustrates this:
265 * var o = new Object();
266 * o.toString(); // returns [object Object]
268 * You can create a function to be called in place of the default `toString()` method. The
269 * `toString()` method takes no arguments and should return a string. The `toString()` method you
270 * create can be any value you want, but it will be most useful if it carries information about the
273 * The following code defines the `Dog` object type and creates `theDog`, an object of type `Dog`:
275 * function Dog(name,breed,color,sex) {
282 * theDog = new Dog("Gabby","Lab","chocolate","female");
284 * If you call the `toString()` method on this custom object, it returns the default value inherited
287 * theDog.toString(); //returns [object Object]
289 * The following code creates and assigns `dogToString()` to override the default `toString()` method.
290 * This function generates a string containing the name, breed, color, and sex of the object, in the
291 * form `"property = value;"`.
293 * Dog.prototype.toString = function dogToString() {
294 * var ret = "Dog " + this.name + " is a " + this.sex + " " + this.color + " " + this.breed;
298 * With the preceding code in place, any time theDog is used in a string context, JavaScript
299 * automatically calls the `dogToString()` function, which returns the following string:
301 * Dog Gabby is a female chocolate Lab
303 * `toString()` can be used with every object and allows you to get its class. To use the
304 * `Object.prototype.toString()` with every object, you need to call `Function.prototype.call()` or
305 * `Function.prototype.apply()` on it, passing the object you want to inspect as the first parameter
308 * var toString = Object.prototype.toString;
310 * toString.call(new Date); // [object Date]
311 * toString.call(new String); // [object String]
312 * toString.call(Math); // [object Math]
314 * @return {String} Object represented as a string.
317 <span id='Object-method-valueOf'>/**
318 </span> * @method valueOf
319 * Returns the primitive value of the specified object.
321 * JavaScript calls the `valueOf` method to convert an object to a primitive value. You rarely need to
322 * invoke the `valueOf` method yourself; JavaScript automatically invokes it when encountering an
323 * object where a primitive value is expected.
325 * By default, the `valueOf` method is inherited by every object descended from `Object`. Every built-
326 * in core object overrides this method to return an appropriate value. If an object has no primitive
327 * value, `valueOf` returns the object itself, which is displayed as:
331 * You can use `valueOf` within your own code to convert a built-in object into a primitive value.
332 * When you create a custom object, you can override `Object.valueOf` to call a custom method instead
333 * of the default `Object` method.
335 * You can create a function to be called in place of the default `valueOf` method. Your function must
338 * Suppose you have an object type `myNumberType` and you want to create a `valueOf` method for it.
339 * The following code assigns a user-defined function to the object's valueOf method:
341 * myNumberType.prototype.valueOf = new Function(functionText)
343 * With the preceding code in place, any time an object of type `myNumberType` is used in a context
344 * where it is to be represented as a primitive value, JavaScript automatically calls the function
345 * defined in the preceding code.
347 * An object's `valueOf` method is usually invoked by JavaScript, but you can invoke it yourself as
352 * Note: Objects in string contexts convert via the `toString` method, which is different from
353 * `String` objects converting to string primitives using `valueOf`. All objects have a string
354 * conversion, if only `"[object type]"`. But many objects do not convert to number, boolean, or
357 * @return {Object} Returns value of the object or the object itself.
362 <span id='Object-property-constructor'>/**
363 </span> * @property constructor
364 * Specifies the function that creates an object's prototype.
366 * Returns a reference to the Object function that created the instance's prototype. Note that the
367 * value of this property is a reference to the function itself, not a string containing the
368 * function's name, but it isn't read only (except for primitive Boolean, Number or String values: 1,
369 * true, "read-only").
371 * All objects inherit a `constructor` property from their `prototype`:
373 * o = new Object // or o = {} in JavaScript 1.2
374 * o.constructor == Object
375 * a = new Array // or a = [] in JavaScript 1.2
376 * a.constructor == Array
378 * n.constructor == Number
380 * Even though you cannot construct most HTML objects, you can do comparisons. For example,
382 * document.constructor == Document
383 * document.form3.constructor == Form
385 * 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`.
387 * function Tree(name) {
390 * theTree = new Tree("Redwood");
391 * console.log("theTree.constructor is " + theTree.constructor);
393 * This example displays the following output:
395 * theTree.constructor is function Tree(name) {
399 * The following example shows how to modify constructor value of generic objects. Only true, 1 and
400 * "test" variable constructors will not be changed. This example explains that is not always so safe
401 * to believe in constructor function.
409 * new Function, function(){},
413 * new RegExp, /(?:)/,
414 * new String, "test"
416 * for(var i = 0; i < types.length; i++){
417 * types[i].constructor = Type;
418 * types[i] = [types[i].constructor, types[i] instanceof Type, types[i].toString()];
420 * alert(types.join("\n"));