When defining a class using the MooTools framework we can include the definition of a constructor inside of it. We just need to add to the object we pass over to Class constructor the ‘initialize’ key with the constructor function as its value.

<!DOCTYPE html>
<html>
<head>
    <title>mootools functions binding demo</title>
    <script type="text/javascript" src="mootools-core-1.4.5-full-compat.js">
    </script>
    <script type="text/javascript">
        var Person = new Class({
            //properties
            name: '',
            id: 0,
            //constructor
            initialize: function(nameVal,idVal)
            {
                this.name = nameVal;
                this.id = idVal;
            },
            //methods
            sleep: function()
            {
                document.write(this.name + ' is sleeping.' + "<br>");
            },
            talk: function()
            {
                document.write(this.name + ' is talking.' + "<br>");
            }
        });
        var a = new Person('david',123123);
        var b = new Person('mike',543543);
        a.talk();
    </script>
</head>
<body>

</body>
</html>

The following video clip shows the execution of this code, overviews it and explains.

Leave a Reply

Your email address will not be published. Required fields are marked *