![]() Also, inside of a method, you can use the symbol to represent the this object. Secondly, coffeescript provides a nice extends syntax for creating a class hierarchy. First is that you can define all of your functions and properties in any order you want, and simply prefix static ones with an symbol. There are several great things about using coffeescript when working with classes, inheritance, and class properties. The first thing to notice above is that class properties are prefixed with an symbol, as you can see in the definition of and In contrast, the move function does not have this prefix, which means it is an instance method, not a static function. This is accomplished by using both instance and class properties and methods. I’ve enhanced Animal to also keep track of the total distance a particular (instance) animal moves as well as the total distance that all animals (class) of each animal type has moved. ![]() It is based on the sample in the coffeescript documentation. The base Animal class defined above is used to display messages about movements that animals make. _.reduce(meters, ((memo, num) -> memo + num), 0) Meters = (val for key, val of Return the sum of all distances moved by all animals Return if Get an array of distances moved by each animal type # Return distance moved by just one type of animal # Static function to retrieve the distance moved by an # Add new distance to total animal type += meters # Retrieve data or initalize this type in = or 0 # Exit if running on the base Animal class # Get the type of anmial to add a distance to # Static function to add a distance to an animal (inst, meters) -> GetDistance: Static storage object to record total distances moved # Get the distance this animal instance moved # console.log Record this distance in static meters # Record distance move by this animal += meters # Display message about animal and distance moved # Distance this animal instance has moved Unfortunately, WordPress doesn’t support coffeescript highlighting. ![]() Below is a more elaborate example in coffeescript. Backbone has excellent coffeescript integration, and working in coffeescript feels much cleaner to me. Now, let’s switch from javascript to coffeescript. It is important to understand this distinction, because a static function can not access an instance’s properties. But when it is used in a static function, it accesses other static properties or functions within that class. Normally, this is reserved for use within instances of a class. They are not instance properties, but static class properties.Īlso note that the setMessage function uses the this keyword. Keep in mind that these values do not change for any given instance of the Sample class. ![]() These class properties may also be referred to as static properties or static methods. In other words, the class properties message and setMessage would be accessed using the Sample class, and not from an instance of that class: If a second parameter is passed to extend, the properties in it become class properties that are attached directly to the constructor function. The first parameter passed to should contain standard Backbone properties, such as initialize, defaults, validate, etc. There is also an example of the Factory pattern, courtesy of Johnny Oshika, that isn’t described in this post.įirst, I’ll start with a basic example in javascript that shows how to define a backbone model with class properties. The source included in this post can be found in my sample-backbone-static github repository. I’ve been exploring this recently, and thought I’d share my findings. Do your Backbone models ever need to share common data between them? Then you might want to consider using class properties and class methods.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |