tag:blogger.com,1999:blog-6442308477719289110.post7788896003175597318..comments2023-05-13T07:27:41.543-07:00Comments on foo(): The this keyword in JavaScriptAlejandro Beltranhttp://www.blogger.com/profile/13533114582122558035noreply@blogger.comBlogger3125tag:blogger.com,1999:blog-6442308477719289110.post-65079316739398364942011-10-15T00:05:01.293-07:002011-10-15T00:05:01.293-07:00I always find the difficulty with the use of subtl...I always find the difficulty with the use of subtle cases with lot of complexity.Please share the best method of learning it simple and easier.<br />Thank you !<br /><a href="http://www.web-designs-company.com" rel="nofollow">web design company</a>Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-6442308477719289110.post-34792226194173062682011-08-01T11:39:03.695-07:002011-08-01T11:39:03.695-07:00Easy way to remember the function/method breakdown...Easy way to remember the function/method breakdown? Functions always use "this" to refer to their parent scope. Since all "global" functions are members of the global object, "this" refers back to them:<br /><br />var window = { bob: function() { conosle.log(this) } }<br />var some_obj = { bob: function() { conosle.log(this) } } <br /><br />Also of note? bind() is new in FF3.6+ Chrome 10+ and IE9+:<br />https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/bind<br /><br />Brilliant stuff!Paul Sweeneyhttps://www.blogger.com/profile/05790372905874351835noreply@blogger.comtag:blogger.com,1999:blog-6442308477719289110.post-68074122435600699882011-07-10T04:36:18.132-07:002011-07-10T04:36:18.132-07:00Don't forget subtle cases such as:
var o = {...Don't forget subtle cases such as:<br /><br /><br />var o = {m: function () { console.log(this); }}<br /><br />o.m(); // o<br />(o.m)(); // o<br /><br />(o.m = o.m)(); // global<br />(o.m || o.m)(); // global<br />(o.m, o.m)(); // global<br /><br />You may find more info on <em>this</em> here: http://dmitrysoshnikov.com/ecmascript/chapter-3-this/Dmitry A. Soshnikovhttps://www.blogger.com/profile/06082291005093539331noreply@blogger.com