{ "cells": [ { "cell_type": "markdown", "id": "f003b793-ddf5-43b7-b557-3c6961a12483", "metadata": {}, "source": [ "### Ce que nous voulons, une classe qui définit un match de dodgeball!" ] }, { "cell_type": "code", "execution_count": 33, "id": "a72f5733-4c3f-4a81-95a7-4ff0f486c292", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[ \u001b[32m\"a\"\u001b[39m, \u001b[32m\"b\"\u001b[39m, \u001b[32m\"e\"\u001b[39m, \u001b[32m\"f\"\u001b[39m, \u001b[32m\"c\"\u001b[39m ]\n", "[ \u001b[32m\"d\"\u001b[39m ]\n" ] } ], "source": [ "\n", "// partie de dodgeball\n", "\n", "class DodgeballGame{\n", "\n", " constructor(joueurs) {\n", " this.joueursEnVie = joueurs;\n", " this.joueursMorts = [];\n", " \n", " }\n", "\n", " kill(joueur){\n", " this.joueursMorts.push(joueur);\n", " const nouveauJoueursEnVie = []\n", " for(const joueurEnVie of this.joueursEnVie){\n", " if(this.joueursMorts.indexOf(joueurEnVie) === -1){\n", " nouveauJoueursEnVie.push(joueurEnVie);\n", " }\n", " }\n", " this.joueursEnVie = nouveauJoueursEnVie;\n", " }\n", "\n", " catchBall(){\n", " if(this.joueursMorts.length < 1){\n", " return;\n", " }\n", " const jesus = this.joueursMorts.shift();\n", " this.joueursEnVie.push(jesus);\n", " }\n", " \n", "}\n", "\n", "let players = [\"a\", \"b\", \"c\", \"d\", \"e\", \"f\"];\n", "\n", "let game = new DodgeballGame([\"a\", \"b\", \"c\", \"d\", \"e\", \"f\"]);\n", "game.kill(\"c\");\n", "game.kill(\"d\")\n", "game.catchBall();\n", "console.log(game.joueursEnVie);\n", "console.log(game.joueursMorts);\n" ] }, { "cell_type": "markdown", "id": "84e2ee1c-d8f5-4145-be1c-2e3775e483ef", "metadata": {}, "source": [ "### Définission premiere classe" ] }, { "cell_type": "code", "execution_count": 2, "id": "b1a24c1a-f1f0-434d-b2d4-a8ddcf668047", "metadata": {}, "outputs": [], "source": [ "class Politicien {\n", "\n", "}\n", "\n", "const politicien = new Politicien();" ] }, { "cell_type": "markdown", "id": "9b2393ec-6ddd-4747-b47b-2a714cb2b7b1", "metadata": {}, "source": [ "### Classe avec méthode simple" ] }, { "cell_type": "code", "execution_count": 12, "id": "72649f7e-c5da-48d9-9e9a-846f084e9cf4", "metadata": {}, "outputs": [], "source": [ "class JackOfAllTrade{\n", " add(a, b){\n", " return a + b;\n", " }\n", "\n", " multiply(a, b){\n", " return a * b;\n", " }\n", "\n", " add1(x){\n", " return this.add(x, 1);\n", " }\n", "}" ] }, { "cell_type": "code", "execution_count": 13, "id": "a8c0005e-2fff-4f50-be35-4dd56bf669e2", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\u001b[33m199\u001b[39m" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "let joat = new JackOfAllTrade()\n", "joat.add(109, 90)" ] }, { "cell_type": "code", "execution_count": 14, "id": "f9c756be-ca43-458a-993d-67652cd5986a", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\u001b[33m101\u001b[39m" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "joat.add1(100)" ] }, { "cell_type": "code", "execution_count": 19, "id": "dda5b8a5-3f3d-40dc-9a02-ca38d970f2ec", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\u001b[32m\"hello_world foifoi\"\u001b[39m" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\n", "class ChangeCase{\n", " \n", " toSnake(str){\n", " str = str.replace(\" \", \"_\");\n", " return str;\n", " }\n", "\n", "}\n", "let case1 = new ChangeCase();\n", "case1.toSnake(\"hello world foifoi\");" ] }, { "cell_type": "code", "execution_count": 1, "id": "29783c57-bcd6-4522-97d7-6fda3bde0d19", "metadata": {}, "outputs": [], "source": [ "let s = \"bonjour monde\"" ] }, { "cell_type": "code", "execution_count": 2, "id": "6fc63875-819c-4d09-b661-90bbdd273f2c", "metadata": {}, "outputs": [], "source": [ "let idx = s.indexOf(\" \")" ] }, { "cell_type": "code", "execution_count": 3, "id": "22adb79c-4c67-45fd-929f-8be1ea3b1c83", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\u001b[32m\"bonjourmonde\"\u001b[39m" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = s.replace(\" \", \"\")" ] }, { "cell_type": "code", "execution_count": 5, "id": "a7bf2d08-6a65-44de-8091-095135b57908", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\u001b[32m\"M\"\u001b[39m" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s.substring(idx, idx + 1).toUpperCase()" ] }, { "cell_type": "code", "execution_count": 8, "id": "8f64482f-2d87-4f0a-b579-050c0bbe477a", "metadata": {}, "outputs": [], "source": [ "import {Point} from \"https://cdn.jsdelivr.net/gh/didiercrunch/cours-420-301-ah@tp1.1/modules-examples/mod.js\"" ] }, { "cell_type": "code", "execution_count": 10, "id": "00102d6b-2997-4fdb-a55f-ed812e1dade1", "metadata": {}, "outputs": [], "source": [ "let point = new Point(34, 78)" ] }, { "cell_type": "code", "execution_count": 11, "id": "9f79bbee-234f-4bd2-9689-77896c847bdd", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\u001b[33m34\u001b[39m" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "point.getX()" ] }, { "cell_type": "code", "execution_count": 12, "id": "9ed8a8a9-303b-4e0b-bdb2-e69b2ea302f6", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\u001b[33m78\u001b[39m" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "point.getY()" ] }, { "cell_type": "code", "execution_count": 13, "id": "a3ff4415-66bf-4b30-858f-90da7b352669", "metadata": {}, "outputs": [], "source": [ "class Point2D{\n", " constructor(x, y){\n", " this.x = x;\n", " this.y = y;\n", " }\n", "\n", " distanceFromOrigin(){\n", " return Math.sqrt(this.x * this.x + this.y * this.y);\n", " }\n", "}" ] }, { "cell_type": "code", "execution_count": 15, "id": "19d175c5-8abd-44d0-8611-33cc543ef608", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\u001b[33m85.0881895447306\u001b[39m" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "let point2D = new Point2D(34, 78)\n", "point2D.distanceFromOrigin()" ] }, { "cell_type": "code", "execution_count": 17, "id": "92669a40-4cea-49a2-b826-1bed8712324a", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "URL {\n", " href: \u001b[32m\"https://example.com/didier/rocks\"\u001b[39m,\n", " origin: \u001b[32m\"https://example.com\"\u001b[39m,\n", " protocol: \u001b[32m\"https:\"\u001b[39m,\n", " username: \u001b[32m\"\"\u001b[39m,\n", " password: \u001b[32m\"\"\u001b[39m,\n", " host: \u001b[32m\"example.com\"\u001b[39m,\n", " hostname: \u001b[32m\"example.com\"\u001b[39m,\n", " port: \u001b[32m\"\"\u001b[39m,\n", " pathname: \u001b[32m\"/didier/rocks\"\u001b[39m,\n", " hash: \u001b[32m\"\"\u001b[39m,\n", " search: \u001b[32m\"\"\u001b[39m\n", "}" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "new URL(\"https://example.com/didier/rocks\")" ] }, { "cell_type": "code", "execution_count": null, "id": "e893b86f-148e-444b-b3dd-503a008c366b", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Deno", "language": "typescript", "name": "deno" }, "language_info": { "file_extension": ".ts", "mimetype": "text/x.typescript", "name": "typescript", "nb_converter": "script", "pygments_lexer": "typescript", "version": "5.2.2" } }, "nbformat": 4, "nbformat_minor": 5 }