JavaScript Page 4 - Form Validation with JavaScript |
You've probably already used JavaScript objects before. For example, the code... <script language="JavaScript"> a = new Image(); </script> ... creates a new instance of the Image object, while... <script language="JavaScript"> x = new Date(); </script> ... creates a new instance of the Date object. JavaScript comes with numerous built-in objects, each with pre-defined methods and properties. But what if you want to roll your own? Well, it isn't very difficult. As a matter of fact, it's almost identical to writing a JavaScript function.
<script language="JavaScript">
// object constructor
function Band()
{
...
}
</script>
You can create an instance of the Band object like this: <script language="JavaScript"> // object instance obj = new Band(); </script> Note the all-important "new" keyword - this is how JavaScript knows that you're trying to create an instance of an object, rather than merely running a function. The code above creates an instance named "obj" of the object Band. You can verify that it is indeed an object by popping up an alert(). <script language="JavaScript"> obj = new Band(); alert(obj); </script>
blog comments powered by Disqus |