Try Out New Website HTML Table Generator

Robot.js

File: Robot.js

/**
 * Modélisation d'un robot
 *
 * @class Robot
 */
var Robot = (function () {
    'use strict';

    // --- Attributs de classe
    /**
     * Directions possibles pour faire marcher un robot
     * @property DIRECTIONS
     * @type Array
     * @static
     */
    Robot.DIRECTIONS = ['NORTH', 'EAST', 'SOUTH', 'WEST'];

    // --- Méthodes de classe
    /**
     * Méthode factory d'un robot
     * @method build
     * @static
     */
    Robot.build = function (name) {
        return new Robot(name);
    };

    // --- Constructeur + attributs d'instance (définis dans le constructeur)
    /**
     * Constructeur
     * @constructor
     * @param {String} name Nom du robot
     */
    function Robot(name) {
        // --- Attributs d'instance
        /**
         * Nom du robot
         * @property name
         * @type String
         */
        this.name = name;

        // --- Traitement fait dans le constructeur
        // On met le nom en capitales
        this.name = this.name.toUpperCase();
    }

    // --- Méthodes d'instance
    /**
     * Méthodes d'instance
     * @property prototype
     * @type Object
     */
    Robot.prototype = {
        /**
         * Un robot peut parler: il sait dire son nom.
         * @method speak
         */
        speak: function () {
            console.log('My name is ' + this.name + '.');
        },

        /**
         * Un robot peut marcher d'une unité dans un direction
         * @method walk
         */
        walk: function (direction) {
            // Vérification de la direction demandée
            if (typeof direction !== 'string' && Robot.DIRECTIONS.indexOf(direction.toUpperCase()) === -1) {
                throw 'Direction argument invalid!';
            }

            console.log('Walk one step in ' + direction + ' direction.');
        }
    };

    // On pense à retourner le constructeur (afin de pouvoir construire des instances, sinon tout
    // le code de notre classe serait inutile car non visible depuis l'extérieur)
    return Robot;
}());


// ---
// Exemples d'utilisation

var mike = new Robot('Mike');
mike.speak();
mike.walk('east');
mike.walk('north');

var titus = Robot.build('Titus');
titus.speak();
titus.walk('north');
titus.walk('north');
titus.walk('east');

console.log('--- Fin fichier Robot.js ------------------');

    (full-width)

Cookies Consent

This website uses cookies to ensure you get the best experience on our website.

Cookies Policy

We employ the use of cookies. By accessing Lantro UI, you agreed to use cookies in agreement with the Lantro UI's Privacy Policy.

Most interactive websites use cookies to let us retrieve the user’s details for each visit. Cookies are used by our website to enable the functionality of certain areas to make it easier for people visiting our website. Some of our affiliate/advertising partners may also use cookies.