|
Classes in JavaScript
JavaScript supports primitives, which I've discussed. It also supports objects, like Array. JavaScript doesn't support classes — at least not in the classical Java language sense. Because JavaScript is a prototype-based language, you don't define classes: instead, behavior is reused via cloning existing objects. Thus, in JavaScript, you don't define class objects, you define them in functions, then use nested functions to define behavior — something you've already seen in action.
To emulate a class, you define a function. You can give it a name (that is, a class name), specify parameters (as in a constructor), and even use the .this keyword, which basically means referencing a variable within the scope of the function. What's more, inner functions can be aliased to look like method calls. |
|