An Object Lesson In JavaScript - Alpha Radiation (Page 4 of 9 )
So far, Sumthing's just sitting around looking pretty - it doesn't actually do anything yet. Let's fix that by modifying it to accept some arguments.
<script language="JavaScript">
// object constructor
function Sumthing(num1, num2) {
// object properties
this.alpha = num1;
this.beta = num2;
}
</script>
The object Sumthing now has two properties, "alpha" and
"beta". With this, it is possible to create an instance of the object Sumthing, and pass it two parameters ("num1" and "num2"), which are then stored as object properties ("alpha" and "beta"). Note my use of the "this" keyword, which provides a convenient way to access variables (and functions) which are "local" to the object constructor.
Let's take it for a spin.
<script language="JavaScript">
obj = new Sumthing(2, 89);
alert("alpha is " + obj.alpha);
alert("beta is " + obj.beta);
</script>
And you should see something like this:


Next: Add()ing Some More >>
More JavaScript Articles
More By Vikram Vaswani, (c) Melonfire