1
0
Fork 0

Class chapter corrections

closure/master
Mike Gerwitz 2011-03-14 18:04:46 -04:00
parent 920f338c98
commit 397a106112
1 changed files with 21 additions and 19 deletions

View File

@ -9,17 +9,17 @@
@node Classes @node Classes
@chapter Working With Classes @chapter Working With Classes
In Object-Oriented programming, the most common term you are likely to hear is In Object-Oriented programming, the most common term you are likely to encounter
``Class''. A @dfn{class} is like a blueprint for creating an @dfn{object}, is ``Class''. A @dfn{class} is like a blueprint for creating an @dfn{object},
which is an @dfn{instance} of that class. Classes contain @dfn{members}, which which is an @dfn{instance} of that class. Classes contain @dfn{members}, which
include primarily properties and methods. A @dfn{property} is a value, much like include primarily properties and methods. A @dfn{property} is a value, much like
a variable, that a class instance ``owns''. A @dfn{method}, when comparing with a variable, that a class ``owns''. A @dfn{method}, when comparing with
JavaScript, is a function that is ``owned'' by an instance. As a consequence, JavaScript, is a function that is ``owned'' by a class. As a consequence,
properties and methods are not part of the global scope. properties and methods are not part of the global scope.
JavaScript does not support classes in the manner in which Object-Oriented JavaScript does not support classes in the manner traditionally understood by
programmers traditionally know. This is because JavaScript follows a different Object-Oriented programmers. This is because JavaScript follows a different
model, which instead uses prototypes. Using this model, JavaScript supports model which instead uses prototypes. Using this model, JavaScript supports
basic instantiation and inheritance. Rather than instantiating classes, basic instantiation and inheritance. Rather than instantiating classes,
JavaScript instantiates constructors, which are functions. The following example JavaScript instantiates constructors, which are functions. The following example
illustrates how you would typically create a class-like object in JavaScript: illustrates how you would typically create a class-like object in JavaScript:
@ -40,9 +40,9 @@ illustrates how you would typically create a class-like object in JavaScript:
// create a new instance of the class and execute doStuff() // create a new instance of the class and execute doStuff()
var foo = new MyClass(); var foo = new MyClass();
console.log( foo.doStuff() ); // outputs "foobar" console.log( foo.getProp() ); // outputs "foobar"
@end verbatim @end verbatim
@caption{Basic ``Class'' in JavaScript} @caption{Basic ``Class'' in JavaScript @emph{without} using ease.js}
@end float @end float
This gets the job done, but the prototypal paradigm has a number of limitations This gets the job done, but the prototypal paradigm has a number of limitations
@ -58,7 +58,7 @@ details should be encapsulated). You, as a developer, should be concerned with
only how to declare and use the classes. If you do not understand what a only how to declare and use the classes. If you do not understand what a
prototype is, that should be perfectly fine. You shouldn't need to understand it prototype is, that should be perfectly fine. You shouldn't need to understand it
in order to use the library (though, it's always good to understand what a in order to use the library (though, it's always good to understand what a
prototype is). prototype is when working with JavaScript).
In this chapter and those that follow, we will see the limitations that ease.js In this chapter and those that follow, we will see the limitations that ease.js
addresses. We will also see how to declare the classes using both prototypes and addresses. We will also see how to declare the classes using both prototypes and
@ -72,7 +72,7 @@ ease.js, until such a point where prototypes are no longer adequate.
@node Declaring Classes @node Declaring Classes
@section Declaring Classes @section Declaring Classes
We just took a look at what it's like declaring a class using prototypes We just took a look at what it's like declaring a class using prototypes
(@pxref{f:class-js,}). This method is preferred for many developers. But it is (@pxref{f:class-js,}). This method is preferred for many developers, but it is
important to recognize that there is a distinct difference between Prototypal important to recognize that there is a distinct difference between Prototypal
and Object-Oriented development models. As an Object-Oriented developer, you and Object-Oriented development models. As an Object-Oriented developer, you
shouldn't concern yourself with @emph{how} a class is declared in JavaScript. In shouldn't concern yourself with @emph{how} a class is declared in JavaScript. In
@ -82,7 +82,7 @@ Let's take a look at how to declare that exact same class using ease.js:
@float Figure, f:class-easejs @float Figure, f:class-easejs
@verbatim @verbatim
// client-side, use: var Class = easejs.Class; // if client-side, use: var Class = easejs.Class;
var Class = require( 'easejs' ).Class; var Class = require( 'easejs' ).Class;
var MyClass = Class( var MyClass = Class(
@ -97,7 +97,7 @@ Let's take a look at how to declare that exact same class using ease.js:
// create a new instance of the class and execute doStuff() // create a new instance of the class and execute doStuff()
var foo = MyClass(); var foo = MyClass();
console.log( foo.doStuff() ); // outputs "foobar" console.log( foo.getProp() ); // outputs "foobar"
@end verbatim @end verbatim
@caption{Basic anonymous class declaration using ease.js} @caption{Basic anonymous class declaration using ease.js}
@end float @end float
@ -107,16 +107,18 @@ couple important notes before we continue evaluating this example:
@itemize @itemize
@item @item
The first thing you will likely notice is our use of the @code{public} keywords. The first thing you will likely notice is our use of the @code{public} keyword.
These are optional (the default visibility is public), but always recommended. This is optional (the default visibility is public), but always recommended.
Future versions of ease.js may provide warnings when the visibility is omitted.
We will get more into visibility later on. We will get more into visibility later on.
@item @item
Unlike @ref{f:class-js,}, we do not use the @code{new} keyword in order to Unlike @ref{f:class-js,}, we do not use the @code{new} keyword in order to
instantiate our class. You are more than welcome to use the @code{new} keyword instantiate our class. You are more than welcome to use the @code{new} keyword
if you wish, but it is optional when using ease.js. This is mainly because, if if you wish, but it is optional when using ease.js. This is mainly because
the keyword is omitted, the constructor is called as a normal function, which without this feautre, if the keyword is omitted, the constructor is called as a
could have highly negative consequences. normal function, which could have highly negative consequences. This style of
instantiation also has its benefits, which will be discussed later on.
@item @item
ease.js's class module is imported using @code{require()} in the above example. ease.js's class module is imported using @code{require()} in the above example.
@ -178,7 +180,7 @@ debugged large JS applications that make liberal use of anonymous functions, you
might be able to understand that frustration. might be able to understand that frustration.
Fortunately, ease.js permits you to declare a named class. A @dfn{named class} Fortunately, ease.js permits you to declare a named class. A @dfn{named class}
is simply a class that is assigned a string for its name, so that errors is simply a class that is assigned a string for its name, so that error
messages, debuggers, etc provide more useful information. @emph{There is messages, debuggers, etc provide more useful information. @emph{There is
functionally no difference between named and anonymous classes.} functionally no difference between named and anonymous classes.}