This project is read-only.

Property Functions

JavaScript does not support a standard way of creating getter and setter functions. getProperty() and setProperty() are a common method, but most .NET developers probably enjoy having a single name to use when getting or setting a property.

NinJa adopts a similar idea with the use of Property Functions. Property functions are functions that accept a single parameter (except for Style, more on that later). When the parameter is provided, a property is set on the object and sometimes code is executed in response to this. When the parameter is missing, the value of the backing field, hidden from the developer, is returned. For example:

$Span("label1").Text("Hello world");

This code sets the text of a span tag with the id "label1" to "Hello world". Then the code gives an alert of what the current Text of the span is, which we know is "Hello world". 

All functions in NinJa that are Property Functions are labeled as such in the Intellisense with <Property Function> preceding the description of the function. One function, Style, always needs the first parameter provided. If the second property is not provided, the current value of the given style is returned, otherwise the style is set to the value provided.

Finally, with property functions, if you are setting the value, the calling object is returned from the function call. This is so that chaining can occur when setting multiple properties:

new Anchor().Text("Search").Href("").Target("_blank");

As you can see, we set each property one after another without every having to create a new line of code.

Last edited Jun 13, 2012 at 5:04 PM by dahrkdaiz, version 2


No comments yet.