API Prototype

Page 145

Chapter

15 Object Object is used by Prototype as a namespace; that is, it just keeps a few new methods together, which are in-

tended for namespaced access (i.e. starting with “Object.”). For the regular developer (who simply uses Prototype without tweaking it), the most commonly used methods are probably inspect and, to a lesser degree, clone. Advanced users, who wish to create their own objects like Prototype does, or explore objects as if they were hashes, will turn to extend, keys and values.

clone Object.clone(obj) -> Object

Clones the passed object using shallow copy (copies all the original’s properties to the result). Do note that this is shallow copy, not deep copy.

var o = { name: 'Prototype', version: 1.5, authors: ['sam', 'contributors'] }; var o2 = Object.clone(o); o2.version = '1.5 weird'; o2.authors.pop(); o.version // -> 1.5 o2.version // -> '1.5 weird' o.authors // -> ['sam'] // Ouch!

Example 15.1.

Shallow copy!


Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.