{"id":87,"date":"2010-04-08T15:50:45","date_gmt":"2010-04-08T13:50:45","guid":{"rendered":"http:\/\/www.tapper-ware.net\/blog\/?p=87"},"modified":"2010-04-14T12:11:37","modified_gmt":"2010-04-14T10:11:37","slug":"calling-methods-in-javascript-without-really-calling-them","status":"publish","type":"post","link":"https:\/\/www.tapper-ware.net\/blog\/calling-methods-in-javascript-without-really-calling-them\/","title":{"rendered":"Calling methods in Javascript, without really calling them"},"content":{"rendered":"<p>Update: I think some people take this a bit too seriously: It&#8217;s a fun little experiment: nothing more, nothing less.<\/p>\n<p>Sometimes you&#8217;re just lazy. Especially when it comes to typing. And while the round brackets in a function call may not seem like a whole lot of overhead, they do get tiresome if you have to type them over and over again&#8230; especially if you don&#8217;t think you need them since the function doesn&#8217;t return anything and needs no arguments: Something that&#8217;s awfully common in object-oriented UI programming.<\/p>\n<p>Surprisingly enough, this isn&#8217;t all that difficult to do in Javascript, since a lot of methods are called implicitly&#8230; toString is probably the most famous one, but there&#8217;s also valueOf that does more or less the same but is called for numeric (and some other) casts.<\/p>\n<p>The simplest way to call it is including a sign:<\/p>\n<pre>+myFunction;<\/pre>\n<p>which effectively translates to<\/p>\n<pre>window.parseFloat(myFunction.valueOf());<\/pre>\n<p>so all we need to do is provide a valueOf method that calls the function itself, like so:<\/p>\n<pre>Function.prototype.valueOf=function(){this.call(this); return 0;};<\/pre>\n<p>and suddenly the plus sing is all that&#8217;s needed for our call.<\/p>\n<p>Especially in an object oriented environment you&#8217;ll have a lot of these calls, but you&#8217;ll have to fix the scope since these methods would be called in the scope of the method itself:<\/p>\n<pre>Function.prototype.fix=function(s){var t=this; return function(){ return t.apply(s,arguments); }; };<\/pre>\n<p>Using this method in the constructor to overwrite each method with a fixed-scope wrapper solves this:<\/p>\n<pre>var Foo=function(){\r\n  this.myMethod=this.myMethod.fix(this);\r\n};<\/pre>\n<p>or a bit more automated:<\/p>\n<pre>var Foo=function(){\r\n  for(var i in this)\r\n    if(typeof(this[i])==\"function\")\r\n      this[i]=this[i].fix(this);\r\n};<\/pre>\n<p>and finally we end up with this whole example (after a bit of OOP refactoring):<\/p>\n<pre>var StandardClass=function(){};\r\n  StandardClass.prototype.initMethods=function(){\r\n    for(var i in this)\r\n      if(typeof(this[i])==\"function\" &amp;&amp; this[i].dontWrap!==true)\r\n        this[i]=this[i].fix(this);\r\n};\r\nStandardClass.prototype.initMethods.dontWrap=true;\r\n\r\nFunction.prototype.fix=function(s){\r\n  var t=this;\r\n  return function(){\r\n    return t.apply(s,arguments);\r\n  };\r\n};\r\n\r\nFunction.prototype.valueOf=function(){\r\n  this.call(this);\r\n  return 0;\r\n};\r\n\r\nvar Foo=function(name){\r\n  this.initMethods();\r\n  this.name=name;\r\n};\r\nFoo.prototype=new StandardClass;\r\n\r\nFoo.prototype.showName=function(){\r\n  alert(this.name);\r\n};\r\n\r\nFoo.prototype.showNameUpperCase=function(){\r\n  alert(this.name.toUpperCase());\r\n};\r\n\r\nvar myFoo=new Foo(\"Hello World\");\r\n\r\n+myFoo.showName;\r\n+myFoo.showNameUpperCase;<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Update: I think some people take this a bit too seriously: It&#8217;s a fun little experiment: nothing more, nothing less. Sometimes you&#8217;re just lazy. Especially when it comes to typing. And while the round brackets in a function call may not seem like a whole lot of overhead, they do get tiresome if you have &hellip; <a href=\"https:\/\/www.tapper-ware.net\/blog\/calling-methods-in-javascript-without-really-calling-them\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Calling methods in Javascript, without really calling them<\/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":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/www.tapper-ware.net\/blog\/wp-json\/wp\/v2\/posts\/87"}],"collection":[{"href":"https:\/\/www.tapper-ware.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tapper-ware.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tapper-ware.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tapper-ware.net\/blog\/wp-json\/wp\/v2\/comments?post=87"}],"version-history":[{"count":3,"href":"https:\/\/www.tapper-ware.net\/blog\/wp-json\/wp\/v2\/posts\/87\/revisions"}],"predecessor-version":[{"id":107,"href":"https:\/\/www.tapper-ware.net\/blog\/wp-json\/wp\/v2\/posts\/87\/revisions\/107"}],"wp:attachment":[{"href":"https:\/\/www.tapper-ware.net\/blog\/wp-json\/wp\/v2\/media?parent=87"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tapper-ware.net\/blog\/wp-json\/wp\/v2\/categories?post=87"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tapper-ware.net\/blog\/wp-json\/wp\/v2\/tags?post=87"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}