{"id":36985,"date":"2018-03-23T03:01:04","date_gmt":"2018-03-22T17:01:04","guid":{"rendered":"http:\/\/www.rjmprogramming.com.au\/ITblog\/?p=36985"},"modified":"2018-03-23T08:50:03","modified_gmt":"2018-03-22T22:50:03","slug":"javascript-instanceof-object-oriented-programming-tutorial","status":"publish","type":"post","link":"https:\/\/www.rjmprogramming.com.au\/ITblog\/javascript-instanceof-object-oriented-programming-tutorial\/","title":{"rendered":"Javascript instanceof Object Oriented Programming Tutorial"},"content":{"rendered":"<div style=\"width: 230px\" class=\"wp-caption alignnone\"><a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/HTMLCSS\/js_instanceof.html\"><img decoding=\"async\" style=\"border: 15px solid pink;\" alt=\"Javascript instanceof Object Oriented Programming Tutorial\" src=\"http:\/\/www.rjmprogramming.com.au\/HTMLCSS\/js_instanceof.jpg\" title=\"Javascript instanceof Object Oriented Programming Tutorial\"  style=\"float:left;\" \/><\/a><p class=\"wp-caption-text\">Javascript instanceof Object Oriented Programming Tutorial<\/p><\/div>\n<p>To understand the Javascript <a target=_blank title='Javascript instanceof information, thanks' href='https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Operators\/instanceof'>instanceof<\/a> operator is to &#8220;get&#8221; a whole lot of what Object Oriented Programming (or <a target=_blank title='OOP information from Wikipedia ... thanks' href='http:\/\/en.wikipedia.org\/wiki\/Object-oriented_programming'>OOP<\/a>) is about.<\/p>\n<p>You&#8217;ve got your concept of a <i>Class<\/i> which is like a &#8220;blueprint&#8221; for describing the characteristics (data members) and methods of an <i>Object<\/i> (and you can think of an <i>Object<\/i> as quite often like the &#8220;Nouns&#8221; of life).<\/p>\n<p>Using that &#8220;blueprint&#8221; <i>Class<\/i> during a web application, using client side Javascript you might use the <b>&#8220;new&#8221;<\/b> keyword like below, and illustrated in that <a target=_blank title='Javascript instanceof information, thanks' href='https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Operators\/instanceof'>instanceof<\/a> link above &#8230;<\/p>\n<blockquote cite='https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Operators\/instanceof'><p>\nfunction Car(make, model, year) {<br \/>\n  this.make = make;<br \/>\n  this.model = model;<br \/>\n  this.year = year;<br \/>\n}<br \/>\nvar auto = <b>new<\/b> Car(&#8216;Honda&#8217;, &#8216;Accord&#8217;, 1998);<br \/>\n<br \/>\nconsole.log(auto <i>instanceof<\/i> Car);<br \/>\n\/\/ expected output: true<br \/>\n<br \/>\nconsole.log(auto <i>instanceof<\/i> Object);<br \/>\n\/\/ expected output: true\n<\/p><\/blockquote>\n<p> &#8230; you are &#8220;instantiating&#8221; the <i>Class<\/i> &#8220;Car&#8221; to the var<font size=1>iable<\/font> &#8220;auto&#8221; above.  That &#8230;<\/p>\n<p><code><br \/>\nfunction Car(make, model, year) {<br \/>\n  this.make = make;<br \/>\n  this.model = model;<br \/>\n  this.year = year;<br \/>\n}<br \/>\n<\/code><\/p>\n<p> &#8230; above is called a &#8220;constructor&#8221; and it can contain more than the &#8220;data members&#8221; it shows above.  It can also define &#8220;methods&#8221;.<\/p>\n<p>Here&#8217;s the good thing about Object Oriented Programming (or OOP) like this.  It begs questions.<\/p>\n<p>What velocity is the <i>Car<\/i> moving?  In the &#8220;blueprint&#8221; <i>Car<\/i> class you would define a &#8220;method&#8221; you might call &#8220;CalculateVelocity&#8221; and then to call that method you might go &#8230;<\/p>\n<p><code><br \/>\nvar car_velocity=auto.CalculateVelocity(distance, time);  \/\/ where v = d \/ t<br \/>\n<\/code><\/p>\n<p> &#8230; via a &#8220;constructor&#8221; <b>method declaration<\/b> like &#8230;<\/p>\n<p><code><br \/>\nfunction Car(make, model, year) {<br \/>\n  this.make = make;<br \/>\n  this.model = model;<br \/>\n  this.year = year;<br \/>\n  <b>this.CalculateVelocity = function(distance, time) {<br \/>\n        return eval(eval('' + distance) \/ eval('' + time));<br \/>\n    };<\/b><br \/>\n}<br \/>\n<\/code><\/p>\n<p>So the Javascript <a target=_blank title='Javascript instanceof information, thanks' href='https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Operators\/instanceof'>instanceof<\/a> operator can be thought of as &#8230;<\/p>\n<blockquote cite='https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Operators\/instanceof'><p>\nThe instanceof operator tests whether the prototype property of a constructor appears anywhere in the prototype chain of an object.\n<\/p><\/blockquote>\n<p>And we have found, if you are at the situation, dynamically executing client side Javascript, and are unsure, Javascript try\/catch code like &#8230;<\/p>\n<p><code><br \/>\ntry {<br \/>\n  alert(auto <i>instanceof<\/i> Car);<br \/>\n} catch (eee) {<br \/>\n  alert('false');<br \/>\n}<br \/>\n<\/code><\/p>\n<p> &#8230; might help you out.<\/p>\n<p>Today&#8217;s web application usage of the Javascript <a target=_blank title='Javascript instanceof information, thanks' href='https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Operators\/instanceof'>instanceof<\/a> operator uses good old <i><a target=_blank title='Javascript DOM document.write method information from w3schools' href='http:\/\/www.w3schools.com\/jsref\/met_doc_write.asp'>document.write<\/a>(<a target=_blank title='Javascript prompt window information from w3schools' href='http:\/\/www.w3schools.com\/jsref\/met_win_prompt.asp'>prompt<\/a>([wording],[default]));<\/i> for that &#8220;in mid air&#8221; prompting feeling to a web application.  It is sort of like a game for two players that goes like this &#8230;<\/p>\n<ol>\n<li>Players navigate to <a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/HTMLCSS\/js_instanceof.html_GETME\" title=\"js_instanceof.html\">js_instanceof.html<\/a>&#8216;s <a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/HTMLCSS\/js_instanceof.html\" title=\"Click picture\">live run<\/a> link<\/li>\n<li>A Javascript prompt window appears and both players can read it but before it is answered, the guessing player should turn away while the other player defines an OOP Class and instantiation example, and then clicks the OK button<\/li>\n<li>At this point the guesser can come back, and try to get inside the other player&#8217;s head, answering and fixing an OOP scenario where any question marks (?) planted there by the web application need to be filled in properly by the guesser in order to receive &#8220;smirking rights&#8221; (no scores today), by clicking the OK Javascript prompt window next presented.<\/li>\n<li>Whether correct or not, the &#8230;<br \/>\n<code><br \/>\n(instantiatedvariablename <i>instanceof<\/i> Classname) = true<br \/>\n<\/code><br \/>\n&#8230; is presented as a conclusion to this round of the game.<\/li>\n<li>The web application restarts, and the players may want to swap roles.<\/li>\n<\/ol>\n<p>If this was interesting you may be interested in <a title='Click here to see topics in which you might be interested' href='#d36985' onclick='var dv=document.getElementById(\"d36985\"); dv.innerHTML = \"&lt;iframe width=670 height=600 src=\" + \"https:\/\/www.rjmprogramming.com.au\/ITblog\/tag\/object\" + \"&gt;&lt;\/iframe&gt;\"; dv.style.display = \"block\";'>this<\/a> too.<\/p>\n<div id='d36985' style='display: none; border-left: 2px solid green; border-top: 2px solid green;'><\/div>\n","protected":false},"excerpt":{"rendered":"<p>To understand the Javascript instanceof operator is to &#8220;get&#8221; a whole lot of what Object Oriented Programming (or OOP) is about. You&#8217;ve got your concept of a Class which is like a &#8220;blueprint&#8221; for describing the characteristics (data members) and &hellip; <a href=\"https:\/\/www.rjmprogramming.com.au\/ITblog\/javascript-instanceof-object-oriented-programming-tutorial\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12,28,33,37],"tags":[2012,652,2010,875,1761,1000,1319],"class_list":["post-36985","post","type-post","status-publish","format-standard","hentry","category-elearning","category-oop","category-software","category-tutorials","tag-instantiation","tag-javascript","tag-object-oriented-programming","tag-oop","tag-prompt","tag-prompt-popup-box","tag-tutorial"],"_links":{"self":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts\/36985"}],"collection":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/comments?post=36985"}],"version-history":[{"count":7,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts\/36985\/revisions"}],"predecessor-version":[{"id":36992,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts\/36985\/revisions\/36992"}],"wp:attachment":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/media?parent=36985"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/categories?post=36985"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/tags?post=36985"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}