Object

11

Click here to load reader

description

Prototype Javascript

Transcript of Object

Page 1: Object

Module

Object

Page 2: Object

Object

• Object is used by Prototype as a namespace • That is, it just keeps a few new methods

together• The most commonly used methods are probably

inspect and, to a lesser degree, clone.

• 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.

Page 3: Object

Object -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

Examples;• 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!  Shallow copy!

 

Page 4: Object

Object-Extend• Object.extend(dest, src) -> alteredDest • Copies all properties from the source to the destination object.

Used by Prototype to simulate inheritance (rather statically) by copying to prototypes.

• The Object.extend function amends an object with the properties of a second object, effectivly combining them.

• Object.extend(objA, objB); // Amends objA with objB • The function also returns the changed objA for direct usage in

an assignment or function call. • Properties in objB override properties of the same name in

objA • A common use for Object.extend() in script.aculo.us is to

update default options with user-supplied overrides.

Page 5: Object

Object-Extend• Examples;• Basic usage:• objA = {name: "Joe", age: "12"};• objB = {name: "Tom"};

• Object.extend(objA, objB);• objA.name• // -> "Tom"  • Usage with assignment:• objA = {name: "Joe", age: "12"};• objC = Object.extend(objA, {name: "Tom"});• objD = Object.extend(objA, {name: "Jim"});

• objC.name• // -> "Tom“• objD.name• // -> "Jim"• // Note that objA always gets changed• objA.name• // -> "Jim

Page 6: Object

Object-inspect

• Object.inspect(obj) -> String Returns the debug-oriented string representation of the object. 

• Undefined and null are represented as such.• Other types are looked up for a inspect method:

if there is one, it is used, otherwise, it reverts to the toString method.

• Prototype provides inspect methods for many types, both built-in and library-defined, such as in String, Array, Enumerable and Hash.

Page 7: Object

Object-inspect• Examples;• Object.inspect()• // -> 'undefined' • Object.inspect(null)• // -> 'null'

• Object.inspect(false)• // -> 'false'

• Object.inspect([1, 2, 3])• // -> '[1, 2, 3]'

• Object.inspect('hello')• // -> "'hello'"

Page 8: Object

Keys

• Object.keys(obj) -> [String...] Treats any object as a Hash and fetches the list of its property names.

• 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.

• Sort the array if you wish to guarantee order.  

Page 9: Object

Keys

• Examples;

• Object.keys()// -> []

• Object.keys({ name: 'Prototype', version: 1.5 }).sort()

• // -> ['name', 'version']

Page 10: Object

Object-toJson

• toJSON() -> String• Returns a JSON string

example;

var data = {name: 'Violet', occupation: 'character', age: 25, pets: ['frog', 'rabbit']};Object.toJSON(data);

 //

> '{"name":"Violet","occupation":"character","age":25,"pets":["frog","rabbit"]}'

 

Page 11: Object

Object-values

• Object.values(obj) -> Array• Treats any object as a Hash and fetches the list of its property values• 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

• property names are unique, property values have no constraint whatsoever Examples;

• Object.values()• // -> []• Object.values({ name: 'Prototype', version: 1.5 }).sort()• // -> [1.5, 'Prototype']