SEARCH
0-9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
Prev | Current Page 102 | Next

Jonathan Snook, Aaron Gustafson, Stuart Langridge, and Dan Webb

"Accelerated DOM Scripting with Ajax, APIs, and Libraries"


function CustomObject(){ }; // or
var CustomObject = function(){ };
With a function, you can use the new keyword to create new objects from your template:
var newObject = new CustomObject();
CHAPTER 3 n OBJECT-ORIENTED PROGRAMMING 58
Languages that have classes have a constructor, which is a special method that gets executed
when the new object is created and can perform some startup duties such as defining
default properties or other actions.
Here??™s an example of a class and a constructor in Java (the important parts are bold):
public class Hello
{
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
Hello notifyTheWorld = new Hello();
First, class Hello declares the name of the class and uses curly braces to contain all the
methods within. The main function gets called every time you create a new object from this
class. Let??™s look at what that code looks like in JavaScript:
function Hello()
{
alert("Hello, World!");
}
var notifyTheWorld = new Hello();
At a quick glance, the two seem similar, yet are quite different. In Java, the main function is
run when instantiating the new object. In JavaScript, however, instantiating the object runs
the function itself. The function is the class definition. The code within the function gets executed
each time you instantiate a new object.
This is important to know because it will affect some of the things you can do when
instantiating and extending your objects.


Pages:
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
hotel jelenia góra Russian bride Free English grammar and study guid powiekszenia wielkoformatowe counter strike 1.6