|
Runtime-Odds and Ends
Page history
last edited
by Charles Jolley 14 years, 2 months ago
- Core object handling methods
- SproutCore defines some code methods to operate on hashes. These are the basis for SproutCore objects but may be useful as utilities as well
- SC.mixin(a,b, [c, [d]]) - copies all properties from 'b' onto 'a', overwriting anything already there.
- SC.copy(a, [b, [c, [d ]]]) - returns a new copy of a. Also copies properties from 'b', 'c', 'd', etc onto copy. same as doing SC.mixin({}, a b, ...)
- SC.merge(a, b) - copies all properties from 'b' onto 'a' unless the property is already defined on 'a'. Useful to adding default properties
- SC.beget(a, [b, [c, [d]]]) - returns a new object that has a as its prototype. also copies properties form b,c,d etc onto a if they are passed. This is basic SC inheritance.
- Guids and Hashes
- SproutCore as a method called SC.guidFor() that will return a unique string ID for ANY object in JavaScript, including strings, numbers, bool, null, undefined, and any Object hash or Array.
- For Object hash or Array, this works by adding a property beginning with '__sc' that contains the guid. Beware that this property may be added to your object at any time
- You can use SC.guidFor() to identify distinct object instances.
- SproutCore also has a method called SC.hashFor() that usually is the same as SC.guidFor() except that it first checks for the presence of a hash() method. If defined, it returns the value of this method instead. This allows you to define objects that are separate instances but logically identical. This is used for comparison tests.
- Comparing Objects
- You should use SC.compare(a,b) to compare two objects. It is more resilient than most others methods - handling all different object types appropriately.
- returns -1 if a is less then b, 1 if b is greater than a, and 0 if they are equal
- Testing Object Types
- use SC.typeOf() to check for object type. This will distinguish between Arrays, different types of Hashes and Objects, among other things. It is more resilient than typeof()
- JSLint (among others) consider it bad form to use == to compare items because it treats null and undefined identically [among other problems].
- Instead, if you want to check than a value is null or undefined use SC.none()
- If you want to check that a value is null, undefined, or an empty string use SC.empty()
Runtime-Odds and Ends
|
Tip: To turn text into a link, highlight the text, then click on a page or file from the list above.
|
|
|
Comments (0)
You don't have permission to comment on this page.