Module Object
Object <ul><li>Object  is used by Prototype as a namespace  </li></ul><ul><li>That is, it just keeps a few new methods tog...
Object -Clone <ul><li>Object.clone(obj) -> Object  Clones the passed object using shallow copy (copies all the original’s ...
Object-Extend <ul><li>Object.extend(dest, src) -> alteredDest   </li></ul><ul><li>Copies all properties from the source to...
Object-Extend <ul><li>Examples; </li></ul><ul><li>Basic usage: </li></ul><ul><li>objA = {name:  &quot;Joe&quot; , age:  &q...
Object-inspect <ul><li>Object.inspect(obj) -> String  Returns the debug-oriented string representation of the object.   </...
Object-inspect <ul><li>Examples; </li></ul><ul><li>Object.inspect() </li></ul><ul><li>// -> 'undefined' </li></ul><ul><li>...
Keys  <ul><li>Object.keys(obj) -> [String...]  Treats any object as a Hash and fetches the list of its property names. </l...
Keys <ul><li>Examples; </li></ul><ul><li>Object.keys() </li></ul><ul><ul><li>// -> [] </li></ul></ul><ul><li>Object.keys({...
Object-toJson <ul><li>toJSON() -> String </li></ul><ul><li>Returns a JSON string </li></ul><ul><li>example; </li></ul><ul>...
Object-values <ul><li>Object.values(obj) -> Array </li></ul><ul><li>Treats any object as a Hash and fetches the list of it...
Upcoming SlideShare
Loading in …5
×

Object

439 views

Published on

Prototype Javascript

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
439
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
6
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Object

  1. 1. Module Object
  2. 2. Object <ul><li>Object is used by Prototype as a namespace </li></ul><ul><li>That is, it just keeps a few new methods together </li></ul><ul><li>The most commonly used methods are probably inspect and, to a lesser degree, clone. </li></ul><ul><li>If want to create their own objects like Prototype does, or explore objects as if they were hashes, will turn to extend, keys and values. </li></ul>
  3. 3. Object -Clone <ul><li>Object.clone(obj) -> Object  Clones the passed object using shallow copy (copies all the original’s properties to the result).   </li></ul><ul><li>Do note that this is shallow copy, not deep copy </li></ul><ul><li>Examples; </li></ul><ul><li>var o = { name: 'Prototype', version: 1.5, authors: ['sam', 'contributors'] }; </li></ul><ul><li>var o2 = Object.clone(o);  </li></ul><ul><li>o2.version = '1.5 weird'; </li></ul><ul><li>o2.authors.pop(); </li></ul><ul><li>o.version </li></ul><ul><li>// -> 1.5  </li></ul><ul><li>o2.version </li></ul><ul><li>// -> '1.5 weird‘ </li></ul><ul><li>o.authors </li></ul><ul><li>// -> ['sam'] // Ouch!  Shallow copy! </li></ul><ul><li>  </li></ul>
  4. 4. Object-Extend <ul><li>Object.extend(dest, src) -> alteredDest </li></ul><ul><li>Copies all properties from the source to the destination object. Used by Prototype to simulate inheritance (rather statically) by copying to prototypes. </li></ul><ul><li>The Object.extend function amends an object with the properties of a second object, effectivly combining them. </li></ul><ul><li>Object.extend(objA, objB); // Amends objA with objB </li></ul><ul><li>The function also returns the changed objA for direct usage in an assignment or function call. </li></ul><ul><li>Properties in objB override properties of the same name in objA </li></ul><ul><li>A common use for Object.extend() in script.aculo.us is to update default options with user-supplied overrides. </li></ul>
  5. 5. Object-Extend <ul><li>Examples; </li></ul><ul><li>Basic usage: </li></ul><ul><li>objA = {name:  &quot;Joe&quot; , age:  &quot;12&quot; }; </li></ul><ul><li>objB = {name:  &quot;Tom&quot; }; </li></ul><ul><li>Object.extend(objA, objB); </li></ul><ul><li>objA.name </li></ul><ul><li>// -> &quot;Tom&quot;  </li></ul><ul><li>  </li></ul><ul><li>Usage with assignment: </li></ul><ul><li>objA = {name:  &quot;Joe&quot; , age:  &quot;12&quot; }; </li></ul><ul><li>objC = Object.extend(objA, {name:  &quot;Tom&quot; }); </li></ul><ul><li>objD = Object.extend(objA, {name:  &quot;Jim&quot; }); </li></ul><ul><li>objC.name </li></ul><ul><li>// -> &quot;Tom“ </li></ul><ul><li>objD.name </li></ul><ul><li>// -> &quot;Jim&quot; </li></ul><ul><li>// Note that objA always gets changed </li></ul><ul><li>objA.name </li></ul><ul><li>// -> &quot;Jim </li></ul>
  6. 6. Object-inspect <ul><li>Object.inspect(obj) -> String  Returns the debug-oriented string representation of the object.   </li></ul><ul><li>Undefined and null are represented as such. </li></ul><ul><li>Other types are looked up for a inspect method: if there is one, it is used, otherwise, it reverts to the toString method. </li></ul><ul><li>Prototype provides inspect methods for many types, both built-in and library-defined, such as in String, Array, Enumerable and Hash. </li></ul>
  7. 7. Object-inspect <ul><li>Examples; </li></ul><ul><li>Object.inspect() </li></ul><ul><li>// -> 'undefined' </li></ul><ul><li>  </li></ul><ul><li>Object.inspect(null) </li></ul><ul><li>// -> 'null' </li></ul><ul><li>Object.inspect(false) </li></ul><ul><li>// -> 'false' </li></ul><ul><li>Object.inspect([1, 2, 3]) </li></ul><ul><li>// -> '[1, 2, 3]' </li></ul><ul><li>Object.inspect('hello') </li></ul><ul><li>// -> &quot;'hello'&quot; </li></ul>
  8. 8. Keys <ul><li>Object.keys(obj) -> [String...]  Treats any object as a Hash and fetches the list of its property names. </li></ul><ul><li>The order of the resulting Array is browser-dependent (it relies on the for...in loop), and is therefore not guaranteed to follow either declaration or lexicographical order. </li></ul><ul><li>Sort the array if you wish to guarantee order.   </li></ul>
  9. 9. Keys <ul><li>Examples; </li></ul><ul><li>Object.keys() </li></ul><ul><ul><li>// -> [] </li></ul></ul><ul><li>Object.keys({ name: 'Prototype', version: 1.5 }).sort() </li></ul><ul><li>// -> ['name', 'version'] </li></ul>
  10. 10. Object-toJson <ul><li>toJSON() -> String </li></ul><ul><li>Returns a JSON string </li></ul><ul><li>example; </li></ul><ul><li>var  data = {name:  'Violet' , occupation:  'character' , age:  25 , pets: [ 'frog' ,  'rabbit' ]}; </li></ul><ul><li>Object.toJSON(data); </li></ul><ul><li>  </li></ul><ul><li>//> '{&quot;name&quot;:&quot;Violet&quot;,&quot;occupation&quot;:&quot;character&quot;,&quot;age&quot;:25,&quot;pets&quot;:[&quot;frog&quot;,&quot;rabbit&quot;]}' </li></ul><ul><li>  </li></ul>
  11. 11. Object-values <ul><li>Object.values(obj) -> Array </li></ul><ul><li>Treats any object as a Hash and fetches the list of its property values </li></ul><ul><li>The order of the resulting Array is browser-dependent (it relies on the for…in loop), and is therefore not guaranteed to follow either declaration or lexicographical order </li></ul><ul><li>property names are unique, property values have no constraint whatsoever </li></ul><ul><li>Examples; </li></ul><ul><li>Object.values() </li></ul><ul><li>// -> [] </li></ul><ul><li>Object.values({ name: 'Prototype', version: 1.5 }).sort() </li></ul><ul><li>// -> [1.5, 'Prototype'] </li></ul>

×