HTML and CSS Reference
In-Depth Information
As you can see, there are only two instance variables that corrospond to the
constructor parameters. The instance variables are prefixed with underscores ( _ )
so that they don't clash with variables passed through the constructor. You also
declare a reference to the instance using _self = this , as the this keyword
references the privileged method and not the object.
The next thing to do is to create an instantiation method, which gets called
toward the end of the model. The code is as follows.
...
app.model.videosource = function appModelVideoSource(url, format){
...
/**
* Set the instance variables using the constructor's arguments
*/
this.init = function(){
this.setUrl(url);
this.setFormat(format);
}
// Insert getters and setters
this.init();
}
As you can see, the init method simply calls the setters for the attributes
passed through the constructor.
NOTE: Variables passed through a constructor are within the scope of
the constructor and privileged methods that are contained within the
constructor. This means that constructor parameters can be used and
modified within privileged methods. When you create new variables
within privileged methods, it's important to declare them using var to
prevent the privileged method from modifying the constructor
parameter.
This prevents code repetition, as you might perform other operations when
modifying instance variables from outside of the object, such as error checking
or changing the value based on a condition.
You might be thinking, why not simply call the setters without wrapping them
with the init method at the top of the JavaScript object as shown in the
 
Search WWH ::




Custom Search