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 š
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
<!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> |