HTML and CSS Reference
In-Depth Information
The code creates a new object with the given parameter as the prototype. We defined the
RectShape
funcion as a constructor. That makes the
RectShape
prototype a new object
along with the
Container
prototype. The following igure shows the prototype relaionship
when we create a new
RectShape
instance:
The following
Mozilla Developer Network
aricle shows an example of class inheritance using
Object.create
:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/
Global_Objects/Object/create
There is one more step to make the inheritance work. The
Container
object has its
own constructor to iniialize its logic. We need to call the parent deiniion funcion or
"constructor" in an object-oriented programming term inside our custom
RectShape
class as follows:
createjs.Container.call(this);
We haven't changed the namespace, so we need the
createjs
namespace whenever we
refer to the
Container
class. In JavaScript, class is a concept. In reality,
Container
is a
funcion object. So we can invoke the
Container
funcion to iniialize it. This has the same
effect of calling
super()
in other class-based, object-oriented programming languages.
The following essay provides an introducion to applying the object-oriented programming
technique in JavaScript:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Introduction_
to_Object-Oriented_JavaScript
In summary, the following simplified code block shows how we can define a new class that
extends an exising one:
function RectShape(arguments){
createjs.Container.call(this); // calling super