{"id":1431,"date":"2013-03-04T00:04:41","date_gmt":"2013-03-04T00:04:41","guid":{"rendered":"http:\/\/www.sitekickr.com\/blog\/?p=1431"},"modified":"2013-03-04T00:18:37","modified_gmt":"2013-03-04T00:18:37","slug":"pranking-prototype","status":"publish","type":"post","link":"https:\/\/www.sitekickr.com\/blog\/pranking-prototype\/","title":{"rendered":"Pranking with Prototype"},"content":{"rendered":"<p>I want to start this post by saying that there really is no practical use for the information presented here. One could argue that it&#39;s a great intro to JavaScript object&#39;s <em>prototype<\/em> property, but it&#39;s really meant to be humorous, the type of thing you do when a deadline isn&#39;t approaching and you really want to drive your colleagues crazy.<\/p>\n<p>To do this properly, you need to have an understanding of JavaScript&#39;s core language feature, prototype. You can do so by searching for <a href=\"http:\/\/www.sitekickr.com\/blog\/?s=prototype\"><em>prototype<\/em><\/a> within this blog, or just google it. This post isn&#39;t too bad:&nbsp;<a href=\"http:\/\/1closure.com\/2012\/07\/object-oriented-javascript-part-2-how-the-prototype-works\/\" target=\"_blank\">http:\/\/1closure.com\/2012\/07\/object-oriented-javascript-part-2-how-the-prototype-works\/<\/a><\/p>\n<p>If you already know how prototype works, you know that it can be used to override &quot;built-in&quot; JavaScript methods and functions. Once you have that understanding, it&#39;s time to get creative.<\/p>\n<p>I would say that 99% of the web applications I develop start with me loading in the jQuery library. Other developers may use other libraries. I bring this up because we&#39;ll need a hiding place for our &quot;prank code&quot;. Within the jQuery library file itself is a great place to tuck the code, as a typical developer would never think twice about checking it.<\/p>\n<p>So, what kind of things can we do to really drive our colleagues crazy?<\/p>\n<h3>String Manipulation<\/h3>\n<ol>\n<li><strong>Override the string replace function.<br \/>\n\t\t<\/strong> <br \/>\n\t\t<code>String.prototype.replace = function() { return &#39;milkshake&#39;; }<\/p>\n<p>\t\ta = &#39;http:\/\/www.example.com&#39;;<\/p>\n<p>\t\ta.replace(&#39;http&#39;, &#39;https&#39;);<\/code><\/p>\n<p>\t\t<code><span style=\"color:#00f;\">milkshake<\/p>\n<p>\t\t<\/span><\/code><\/li>\n<li><strong>Override the a case change function.<\/strong>\n<p>\t\t<code>String.prototype.toLowerCase = function() { return this.toUpperCase(); }<\/p>\n<p>\t\t&#39;Test&#39;.toLowerCase()<\/p>\n<p>\t\t<span style=\"color:#00f;\">&quot;TEST&quot;<\/p>\n<p>\t\t<\/span><\/code><\/li>\n<li><strong>Play with the built-in trim function<\/strong> so it actually doubles the amount of white space padding, instead of removing it.\n<p>\t\t<code><kbd>String.prototype.trim = function() { return this.replace(\/ \/g, &#39; &#39;); }<\/p>\n<p>\t\t&#39; &nbsp;test &nbsp;&#39;.trim()<\/p>\n<p>\t\t<span style=\"color:#00f;\">&quot; &nbsp; &nbsp;test &nbsp; &nbsp;&quot;<\/p>\n<p>\t\t<\/span><\/kbd><\/code><\/li>\n<\/ol>\n<p>You get the idea. You can be pretty devious and really drive a fellow developer crazy trying to figure out why his code keeps outputting the string &quot;milkshake&quot;!<\/p>\n<p>Unfortunately, JavaScript doesn&#39;t have a very extensive collection of built-in functions. What&#39;s worse, is that you can&#39;t override the Math object functions. So, you really need to get creative!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I want to start this post by saying that there really is no practical use for the information presented here. One could argue that it&#39;s&hellip;<\/p>\n","protected":false},"author":1,"featured_media":1435,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"amp_status":""},"categories":[219,238,5,122],"tags":[227],"_links":{"self":[{"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/posts\/1431"}],"collection":[{"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/comments?post=1431"}],"version-history":[{"count":5,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/posts\/1431\/revisions"}],"predecessor-version":[{"id":1437,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/posts\/1431\/revisions\/1437"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/media\/1435"}],"wp:attachment":[{"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/media?parent=1431"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/categories?post=1431"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/tags?post=1431"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}