JS – OOP in JS, made simple

JavaScript seems to be the language for me for December. Although I am really working with VBA only, JS has really a lot to show. Thus, today I will simply write some easy OOP samples, that I can use later 🙂

JS

So, the code for today does the following – it shows four messageboxes, each one with some information about an object.

We have two objects – first_article and second_article and they are both from type Article. Type Article has two functions  – like_yourself_in_fb & info_about_article. So, when you call each of them, you get a message box with some information. Enough is enough, here comes the code:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8"/>
    <title>OOP VitoshAcademy</title>
    <script>
        function Article(title, language, writer) {
            this.title = title;
            this.language = language;
            this.writer = writer;
        }
        Article.prototype = {
            like_yourself_in_fb: function () {
                alert(this.writer + " has liked in facebook " + this.title);
            },
            info_about_article: function (paragraphs_number) {
                alert(this.title + " " + paragraphs_number + " paragraphs!");
            }
        };
        var first_article = new Article("VBA is fun!", "VBA", "Vitosh");
        var second_article = new Article("C# is not that easy!", "C#", "Pesho");
        second_article.number_of_paragraphs = 15;
        first_article.has_pic = false;
        first_article.like_yourself_in_fb();
        second_article.like_yourself_in_fb();
        first_article.info_about_article(5);
        second_article.info_about_article(12);
    </script>
    </head>
<body>
<h1>Click refresh to see the 4 msgboxes once again!</h1>
<noscript>JS is not activated.</noscript>
</body>
</html>

Enjoy it in GitHub as well 🙂