Created
April 11, 2010 13:38
-
-
Save phiggins42/362717 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function(d){ | |
d.Timer = function(args){ | |
// summary: The constructor function. Anytime we `new` up a Timer, | |
// this function is called, passing anything passed. | |
// | |
// example: | |
// | var x = new dojo.Timer({ a:b }); | |
// | x.a == b // true | |
d.mixin(this, args); | |
}; | |
// add default values and member function to the Timer prototype: | |
d.extend(d.Timer, { | |
// value: Int | |
// Some value | |
value:10, | |
add: function(val){ | |
// summary: Add some passed `val` to this value. | |
// returns: Ourself, to allow chaining. | |
this.value += val; | |
return this; // dojo.Timer | |
}, | |
show:function(){ | |
// summary: Display our value | |
console.log(this.value); | |
} | |
}); | |
// note: use full `dojo` here in string form, d is local | |
d.declare("dojo.TimerTwo", d.Timer, { | |
// summary: A Sub-class of a Timer, providing an overridden | |
// `add` behavior. | |
add: function(val){ | |
// summary: Add some passed `val` to this value | |
// but also subtract half the value of `val` | |
// returns: Ourself, to allow chaining. | |
this.inherited(arguments); | |
this.value -= (val / 2); | |
return this; // dojo.TimerTwo | |
} | |
}); | |
// tests: | |
var ti = new d.Timer(), | |
tt = new d.TimerTwo(), | |
ttt = new d.Timer({ value: 20 }) | |
; | |
ti.add(10).show(); // adds 10, shows 20 | |
tt.add(10).show(); // adds 10, then subtracts 5, shows 15 | |
ttt.add(1).show(); // adds 1 to default of 20, shows 21 | |
})(dojo); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment