{"id":4381,"date":"2016-03-01T12:59:08","date_gmt":"2016-03-01T12:59:08","guid":{"rendered":"http:\/\/www.hostingnotes.com\/?p=4381"},"modified":"2017-10-09T13:55:51","modified_gmt":"2017-10-09T13:55:51","slug":"javascript-guide-object-oriented-programming-in-javascript","status":"publish","type":"post","link":"https:\/\/www.webdevelopersnotes.com\/javascript-guide-object-oriented-programming-in-javascript","title":{"rendered":"JavaScript Guide &#8211; Object Oriented Programming in JavaScript"},"content":{"rendered":"<p>Object Oriented Programming is a relatively new concept in the computing field. It is supposed to make life simpler and easier for the programmer, however, the newly initiated get confused quickly. I too was disconcerted at the beginning even though I had a good programming experience with BASIC and FORTRAN. The concept is actually quite simple and if you read this session well, I assure you that you will soon grasp it.<\/p>\n<p><strong>Note:<\/strong> JavaScript is not a true Object Oriented language as C++ or Java but rather an <strong>Object Based<\/strong> language.<\/p>\n<h2>Its a wild world out there<\/h2>\n<p>Before I start describing objects in programming aspect, let&#8217;s look some pictures.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"\/\/www.webdevelopersnotes.com\/tutorials\/javascript\/bear2.gif\" width=\"75\" height=\"61\" alt=\"Bear 1\" class=\"alignleft\"\/><img loading=\"lazy\" decoding=\"async\" src=\"\/\/www.webdevelopersnotes.com\/tutorials\/javascript\/panda.gif\" width=\"75\" height=\"53\" alt=\"Panda\" class=\"alignleft\"\/><img loading=\"lazy\" decoding=\"async\" src=\"\/\/www.webdevelopersnotes.com\/tutorials\/javascript\/bear3.gif\" width=\"45\" height=\"60\" alt=\"Bear 3\" class=\"alignleft\"\/><\/p>\n<div class=\"clr\"><\/div>\n<p>Here are two ferocious bears and a docile bamboo eating panda. They are animals that we find in a zoo (Its not pleasant to come across the grizzly in the wild)<\/p>\n<h2>Are animals objects?<\/h2>\n<p>Yes! The Wildlife Foundation might disapprove of this&#8230; but then, I guess it&#8217;s harmless if we just take it as a analogy!<br \/>\nWe see objects all around us, the chair we sit on, the table on which our computer is placed and even other humans.<\/p>\n<h2>Object Properties<\/h2>\n<p>To describe a bear or a panda, we specify its characteristics such as color, size, weight, eating habits, the place where its found etc. These are <strong>properties<\/strong> of the animal.<\/p>\n<p><strong>Polar Bears<\/strong><br \/>\nColor: White<br \/>\nDiet: Fish, seals&#8230; non-vegetarian<br \/>\nLocation: The North Tundra<\/p>\n<p><strong>Panda<\/strong><br \/>\nColor: White and Black<br \/>\nDiet: Bamboo shoots<br \/>\nLocation: China<\/p>\n<h2>Object Methods<\/h2>\n<p>So what are the things a polar bear can do? It eats, runs, swims, hibernates in the winter season etc. A Panda also eats and runs but it can also climbs trees and seldom swims!<br \/>Such actions are the functions of the animal object. Functions in object programming are called <strong>methods<\/strong>.<\/p>\n<p>Thus, an object can be anything, a living being, an inanimate item, a city or even an idea. An object is described by its characteristics called properties and can preform certain functions called methods.<\/p>\n<h2>JavaScript Objects<\/h2>\n<p>Objects in JavaScript are software entities such as the browser window or an HTML document.<br \/>\nLet&#8217;s describe the browser window object and some its properties and methods.<br \/>\nA browser window object is created whenever a document is loaded in a browser.<\/p>\n<p><strong>Its Properties<\/strong><\/p>\n<ul class=\"withbullets\">\n<li>Width<\/li>\n<li>Height<\/li>\n<li>Name<\/li>\n<li>The URL of the document it displays<\/li>\n<\/ul>\n<p><strong>Its Methods<\/strong><\/p>\n<ul class=\"withbullets\">\n<li>Open<\/li>\n<li>Close<\/li>\n<li>Resize<\/li>\n<\/ul>\n<p><\/p>\n<p>So, you can define a <strong>window<\/strong> object with its properties such as width, height, name etc. The behaviour of the window can be controlled using its methods such as open, close, resize etc.<\/p>\n<p>The document displayed by the browser window object is itself an object with its own properties and methods. Thus, the <strong>document<\/strong> object is contained in the <strong>window<\/strong> object.<\/p>\n<p>Consider an analogy of our galaxy (Milky Way) object that consists of other objects such as our solar system which further contains objects such as the Earth and the Sun. An easy way to describe the Earth to an alien residing in some other galaxy would be:<\/p>\n<p><strong>Milky way &#8211; Solar System &#8211; Earth<\/strong><\/p>\n<p>Note that the above can also be written as:<\/p>\n<p><strong>Universe &#8211; Milky Way &#8211; Solar System &#8211; Earth<\/strong><\/p>\n<p>But since there is only one universe (hope so!), we can leave out the redundant term. Thus, the Milky Way is a parent object to the Solar System which is itself a parent to the Earth.<\/p>\n<p>JavaScript uses the period to separate the parent object from its components. Hence, to refer to the document object we use the following notation:<\/p>\n<pre class=\"small-text\"><strong>window.document<\/strong><\/pre>\n<p>A HTML Form called <strong>contactform<\/strong> inside the document might be referred as:<\/p>\n<pre class=\"small-text\"><strong>window.document.contactform<\/strong><\/pre>\n<p>For convenience, webdevelopers name HTML elements using the NAME attribute. Thus, a text field named <strong>address<\/strong> located in a form named <strong>contactform<\/strong> is refered as:<\/p>\n<pre class=\"small-text\"><strong>window.document.contactform.address<\/strong><\/pre>\n<p>or a radio button called <strong>answer<\/strong> as:<\/p>\n<pre class=\"small-text\"><strong>window.document.contactform.answer<\/strong><\/pre>\n<p>or text field called <strong>name<\/strong> as:<\/p>\n<pre class=\"small-text\"><strong>window.document.contactform.name<\/strong><\/pre>\n<p>As you would have noticed, the &#8216;window&#8217; object is the parent to all other objects, so it is not necessary to explicitly specify it.<\/p>\n<div class=\"pagenav\">\n<ul>\n<li><a href=\"\/\/www.webdevelopersnotes.com\/javascript-introduction-writing-javascript-with-html\" title=\"Javascript introduction - writing javascript with HTML\">&lt; Back<\/a><\/li>\n<li>&#8230;<\/li>\n<li><a href=\"\/\/www.webdevelopersnotes.com\/javascript-guide-object-oriented-programming-in-javascript\" title=\"Online javascript guide to object oriented programming in javascript\" class=\"thispage\">5<\/a><\/li>\n<li>&#8230;<\/li>\n<li><a href=\"\/\/www.webdevelopersnotes.com\/javascript-event-handlers-part-1\" title=\"Javascript onmouseover and javascript onmouseout event handlers\">10<\/a><\/li>\n<li>&#8230;<\/li>\n<li><a href=\"\/\/www.webdevelopersnotes.com\/javascript-if-statement\" title=\"Javascript IF statement\">20<\/a><\/li>\n<li>&#8230;<\/li>\n<li><a href=\"\/\/www.webdevelopersnotes.com\/changing-images-on-mouseover-using-javascript\" title=\"Changing images on mouseover using Javascript\">30<\/a><\/li>\n<li>&#8230;<\/li>\n<li><a href=\"\/\/www.webdevelopersnotes.com\/understanding-javascript-objects-online-javascript-help\" title=\"Online javascript help - Understanding javascript objects\">Next &gt;<\/a><\/li>\n<\/ul>\n<\/div>\n","protected":false},"excerpt":{"rendered":"Object Oriented Programming is a relatively new concept in the computing field. It is supposed to make life simpler and easier for the programmer, however, the newly initiated get confused quickly. I too was disconcerted at the beginning even though I had a good programming experience with BASIC and FORTRAN. The concept is actually quite [&hellip;]","protected":false},"author":1,"featured_media":15642,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[56],"tags":[],"class_list":["post-4381","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-javascript"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.webdevelopersnotes.com\/wp-json\/wp\/v2\/posts\/4381","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.webdevelopersnotes.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.webdevelopersnotes.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.webdevelopersnotes.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.webdevelopersnotes.com\/wp-json\/wp\/v2\/comments?post=4381"}],"version-history":[{"count":0,"href":"https:\/\/www.webdevelopersnotes.com\/wp-json\/wp\/v2\/posts\/4381\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.webdevelopersnotes.com\/wp-json\/wp\/v2\/media\/15642"}],"wp:attachment":[{"href":"https:\/\/www.webdevelopersnotes.com\/wp-json\/wp\/v2\/media?parent=4381"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.webdevelopersnotes.com\/wp-json\/wp\/v2\/categories?post=4381"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.webdevelopersnotes.com\/wp-json\/wp\/v2\/tags?post=4381"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}