{"id":4157,"date":"2019-04-10T19:38:44","date_gmt":"2019-04-10T19:38:44","guid":{"rendered":"http:\/\/www.nikola-breznjak.com\/blog\/?p=4157"},"modified":"2019-04-10T19:38:44","modified_gmt":"2019-04-10T19:38:44","slug":"intro-oop-javascript","status":"publish","type":"post","link":"https:\/\/nikola-breznjak.com\/blog\/javascript\/intro-oop-javascript\/","title":{"rendered":"Intro to OOP in JavaScript"},"content":{"rendered":"<h2>TL;DR<\/h2>\n<p>This post will not be a regular step by step post as I usually do it; this will rather be a collection of some notes from a recent lecture I did. However, I hope you still find it useful.<\/p>\n<h2>OOP &#8211; Object Oriented Programming<\/h2>\n<ul>\n<li>an object has a <code>state<\/code> (represented by <code>properties<\/code>) and a <code>behavior<\/code> (represented by <code>methods<\/code>)<\/li>\n<li><code>functions<\/code> on an object are called <code>methods<\/code><\/li>\n<li>a developer doesn&#8217;t need to know how an object\u2019s methods work under the hood to be able to use them (encapsulation)<\/li>\n<li>a lot of times it makes sense to model real-life objects as objects in the code<\/li>\n<\/ul>\n<blockquote><p>\n  \u26a0\ufe0f interesting thing in JavaScript is that <code>arrays<\/code> are actually objects<\/p>\n<p>  You can test it like this: <code>console.log(typeof []);<\/code><\/p>\n<p>  It&#8217;s an object because it has a property like <code>length<\/code>, and methods like <code>pop<\/code> and <code>push<\/code>\n<\/p><\/blockquote>\n<h2>object literal<\/h2>\n<pre><code>const person = {\n    name: 'Nikola',\n    age: 33,\n    walk: function() {\n        console.log(\"I'm walking\");\n    }\n}\n\nconsole.log(person.name);\n<\/code><\/pre>\n<p>There&#8217;s also this thing called &#8216;bracket notation&#8217;:<\/p>\n<pre><code>person['name']\n\nperson['walk']();\n<\/code><\/pre>\n<p>Here&#8217;s how you would output all of the object&#8217;s properties with jQuery:<\/p>\n<pre><code>$.each(person, function(i,o){\n    console.log(i, o);\n});\n<\/code><\/pre>\n<h2><code>this<\/code> in JavaScript<\/h2>\n<ul>\n<li>normal function calls<\/li>\n<\/ul>\n<pre><code>function f(){\n    console.log(this); \/\/ Window object\n}\n\nf();\n<\/code><\/pre>\n<ul>\n<li>object methods<\/li>\n<\/ul>\n<pre><code>var obj = {\n    a: \"test\",\n    b: \"test 2\",\n    log: function() {\n        console.log(this.a);\n    }\n}\n\nobj.log();\n<\/code><\/pre>\n<ul>\n<li>constructed object methods<\/li>\n<\/ul>\n<pre><code>var Person = function(name, surname) {\n    this.name = name || 'Marko';\n    this.surname = surname || 'Maric';\n\n    this.log = function() {\n        console.log(this.name + \" \" + this.surname);\n    }\n}\n\nvar pero = new Person('Pero', 'Peric'); \/\/ constructor function\npero.log();\n<\/code><\/pre>\n<h2>JavaScript Class Syntax<\/h2>\n<p>Object literals fall short when you have to create multiple of them. Having to type each of them out and then in case of a change, change it in all the places would be unmanageable.<\/p>\n<p><code>Class<\/code> is basically a blueprint for <code>objects<\/code> that will be created with this blueprint. It contains some base set of <code>properties<\/code> and <code>methods<\/code>. The <code>class<\/code> syntax is new to JS in <code>ES2015<\/code> and it&#8217;s actually something called &#8216;syntactic sugar&#8217;. It has the same functionality as the <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Object\/prototype\">prototype<\/a> syntax, and it&#8217;s actually still using prototypes under the hood.<\/p>\n<pre><code>class Pet {\n    constructor(animal, age, bread, sound) {\n        this.animal = animal;\n        this.age = age;\n        this.bread = bread;\n        this.sound = sound;\n    }\n\n    speak() {\n        console.log(this.sound);\n    }\n}\n\nconst djoni = new Pet('dog', 1, 'bulldog', 'vau vau');\n\nconsole.log(djoni);\n<\/code><\/pre>\n<h3>getters<\/h3>\n<pre><code>class Pet {\n    constructor(animal, age, bread, sound) {\n        this.animal = animal;\n        this.age = age;\n        this.bread = bread;\n        this.sound = sound;\n    }\n\n    get activity() {\n        const today = new Date();\n        const hour = t.getHours();\n\n        if (hour &gt; 8 &amp;&amp; hour &lt; 20) {\n            return 'playing';\n        }\n        else {\n            return 'sleeping';\n        }\n    }\n\n    speak() {\n        console.log(this.sound);\n    }\n}\n\nconst djoni = new Pet('dog', 1, 'bulldog', 'vau vau');\n\nconsole.log(djoni.activity); \/\/ activity is a so-called 'computed property'.\n<\/code><\/pre>\n<h3>setters<\/h3>\n<pre><code>get owner() {\n    return this._owner.\n}\n\nset owner(owner) { \/\/ MUST have exactly one formal parameter!\n    this._owner = owner; \/\/ this is called a 'backing property'\n}\n\ndjoni.owner = 'Nikola';\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>TL;DR This post will not be a regular step by step post as I usually do it; this will rather be a collection of some notes from a&hellip;<\/p>\n","protected":false},"author":1,"featured_media":4158,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[27],"tags":[],"class_list":["post-4157","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-javascript"],"_links":{"self":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts\/4157","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/comments?post=4157"}],"version-history":[{"count":1,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts\/4157\/revisions"}],"predecessor-version":[{"id":4159,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts\/4157\/revisions\/4159"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/media\/4158"}],"wp:attachment":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/media?parent=4157"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/categories?post=4157"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/tags?post=4157"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}