• If you are citizen of an European Union member nation, you may not use this service unless you are at least 16 years old.

  • You already know Dokkio is an AI-powered assistant to organize & manage your digital files & messages. Very soon, Dokkio will support Outlook as well as One Drive. Check it out today!

View
 

Basics-UtilityMethods

Page history last edited by Kevin Kleinfelter 14 years, 5 months ago

In addition to its higher-level code, SproutCore also includes a number of useful utility methods that can make your code tighter and more efficient.  It is useful to get to know them.

 

SC.typeOf()

 

Returns a constant indicating the type of object passed in.  JavaScript is a duck-typed language, which means there are many different ways to detect type; some are more robust than others.  This method provides a consistent, reliable implementation across all browsers.

 

SC.guidFor()

 

Generates a unique guid for almost any unique JavaScript object including primitives such as numbers, strings, SproutCore Objects and generic JS objects.  This method is generally very fast and therefore it is a good way to quickly compare two objects for equality.  Do not use on DOM elements.

 

SC.hashFor()

 

Works just like guidFor() except, if the object you pass in implements the hash() method, it will return the return value of that method instead of the object guid.  This allows you to create objects that are technically separate instances but evaluate as equal.  Do not use on DOM elements.

 

SC.isEqual()

 

Compares two objects, using their hash code or guids if available.  This method is more robust than the built-in === or ==.

 

SC.objectForPropertyPath() 

 

Parses a property path and returns the object pointed to by that path if any.  This method also accepts an optional root object and can handle relative-style property paths such as "this.foo" or ".foo".  Use this method to evaluate property paths instead of evaling.

 

SC.tupleForPropertyPath() 

 

Just like objectForPropertyPath() except this method expects the last component of the property path to name a property or method you want to access on the object.  For example "MyApp.Foo.bar" would return an array with [MyApp.Foo, "bar"] (first element is the object itself, not a string)

 

SC.maxX(), SC.minX(), SCmidX(), SC.maxY(), SC.minY(), SCmidY() 

 

Accept a rect structure (with x, y, width, and height properties expressed as numbers) and return the relevant value.  Useful for rect calculations.

 

SC.minRange(), SC.maxRange() 

 

Accept a range structure (with start and length properties expressed as numbers) and return the minimum value and maximum value respectively.

 

SC.none()

 

Returns YES if the passed value is null or undefined.  This avoids errors from JSLint complaining about use of ==, which can be technically confusing.

 

SC.isArray()

 

Returns YES if the passed object is an array or array-like. Instances of the NodeList class return NO.

Unlike SC.typeOf this method returns true even if the passed object is not formally array but appears to be array-like (i.e. has a length property, responds to .objectAt, etc.)

 

SC.inspect()

 

Convenience method to inspect an object.  This method will attempt to convert the object into a useful string description.

 

 

 

 

See the built-in documentation for the complete reference of these functions.  For more useful programming guidelines see the JavaScript Style Guide as well.

 

 

Comments (0)

You don't have permission to comment on this page.