Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var MyNamedThing = Class.create({
- initialize: function(name) {
- // The 'name' parameter is a private variable, no one can see it from the outside
- // Here's another private variable:
- var description;
- // 'description' is the same as 'name' unless the caller sets one explicitly
- // We do a fairly silly implementation of that to make the point about private vars and accessors
- description = name;
- // Public accessors for 'name'
- this.getName = function() {
- return name;
- };
- this.setName = function(newname) {
- if (!newname) {
- throw "Invalid name";
- }
- if (description == name) {
- description = newname;
- }
- name = newname;
- };
- // Public accessors for 'description'
- this.getDescription = function() {
- return description;
- };
- this.setDescription = function(newdesc) {
- if (!newdesc) {
- throw "Invalid description";
- }
- description = newdesc;
- };
- }
- });
Add Comment
Please, Sign In to add comment