Prev: FAQ Topic - Why does simple decimal arithmetic give strange results? (2010-03-31)
Next: Consolidate Credit Card Debt
From: johnwlockwood on 1 Apr 2010 16:05 On Apr 1, 1:11 pm, Thomas 'PointedEars' Lahn <PointedE...(a)web.de> wrote: > >> The `this' in the method definition is OK; the method definition is > > ^^^^^^^^^^^^^^^^^^^^^^^^>> pointless, of course, as both `getId_' and `getId' would be visible > > ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^>> outside of the constructor's execution context. > > > var alert; > > You don't want to do that. Ok, the only reason I did is jslint.com gave the error "Implied global: alert" > > > function MyObject(id) > > { > > var me = this; > > me.id = id; > > me.getId_ = function() > > { > > return me.getId(); > > }; > > } > > > MyObject.prototype.getId = function() > > { > > var me = this; > > return me.id; > > }; > > You do see that this does not make sense anyway, don't you? Oh yeah, I didn't write it to make sense, just a short illustration of my understanding of how I should create methods and make them a closure. I learned this style mostly from working with http://gmaps-utility-library.googlecode.com/svn/trunk/markermanager/1.0/src/markermanager.js > > but it is my understanding that defining methods of > > and object using prototype was a good thing. > > If you make use of the prototype chain that makes sense. You do not. Okay. > You are calling a prototype method from a method of an instance here when > you should have called the prototype method *as* method of the instance\ > instead (so that lookup along the prototype chain would have called that > method automatically). I don't understand. Are you saying I should have written: var getMyObjectId = myObject1.getId; which "getMyObjectId();" will call MyObject.prototype.getId? > > > And if getId is defined under the Object prototype: In order to say, > > specify the myObject.getId function as a event handler, you must make a > > private member a function that calls the prototype method, or else 'this' > > will be the global context. > > You use the terms without understanding their meaning. I don't even know > where to begin correcting this nonsense. Sorry about using the wrong terms for what I mean, I will work on improving my understanding of the correct terms. By 'global context', I meant that 'this' would be of the Global object. -John
From: Scott Sauyet on 1 Apr 2010 16:38 nick wrote: > Look at this code: > > myApp.keys.register > ('w', panNorth) > ('s', panSouth) > ('a', panWest) > ('d', panEast); > > > What else could it possibly be doing besides registering a bunch of > key handlers? It works and it makes sense, so it's not entirely > ridiculous. And, really, how is it any worse than something like this: > > myApp.keys.register([ > ['w', panNorth], > ['s', panSouth], > ['a', panWest], > ['d', panEast] > ]); I agree with the other posters that it is in fact less clear. But there are other options as well: myApp.keys.register({ 'w': panNorth, 's': panSouth, 'a': panWest, 'd': panEast }); or myApp.keys.register( 'w', panNorth, 's', panSouth, 'a', panWest, 'd', panEast ); either of which feels a little more normal Javascript-y to me. -- Scott
From: Antony Scriven on 1 Apr 2010 16:56 On Apr 1, 9:38 pm, Scott Sauyet <scott.sau...(a)gmail.com> wrote: > nick wrote: > > Look at this code: > > > myApp.keys.register > > ('w', panNorth) > > ('s', panSouth) > > ('a', panWest) > > ('d', panEast); > > > What else could it possibly be doing besides > > registering a bunch of key handlers? It works and it > > makes sense, so it's not entirely ridiculous. And, > > really, how is it any worse than something like this: > > > myApp.keys.register([ > > ['w', panNorth], > > ['s', panSouth], > > ['a', panWest], > > ['d', panEast] > > ]); > > I agree with the other posters that it is in fact less > clear. But there are other options as well: > > myApp.keys.register({ > 'w': panNorth, > 's': panSouth, > 'a': panWest, > 'd': panEast > }); > > or > > myApp.keys.register( > 'w', panNorth, > 's', panSouth, > 'a', panWest, > 'd', panEast > ); > > either of which feels a little more normal Javascript-y to me. Agreed. You could also probably do the following. myApp.keys = { 'w': panNorth, 's': panSouth, 'a': panWest, 'd': panEast }; --Antony
From: Thomas 'PointedEars' Lahn on 1 Apr 2010 17:44 johnwlockwood wrote: > Thomas 'PointedEars' Lahn wrote: >> > var alert; >> >> You don't want to do that. > > Ok, the only reason I did is jslint.com gave the error "Implied > global: alert" You probably get the error because you called alert(...); not because you have not declared it. Call window.alert(...); and take everything jslint says with a handful of salt. >> > function MyObject(id) >> > { >> > var me = this; >> > me.id = id; >> > me.getId_ = function() >> > { >> > return me.getId(); >> > }; >> > } >> >> > MyObject.prototype.getId = function() >> > { >> > var me = this; >> > return me.id; >> > }; >> >> You do see that this does not make sense anyway, don't you? > > Oh yeah, I didn't write it to make sense, just a short illustration of > my understanding of how I should create methods and make them a > closure. What are you talking about? There is exactly *no* point of having me.getId_() here as both (new MyObject("a")).getId_() and (new MyObject("a")).getId() work and do *exactly* the same. You can also lose the bogus `me' in getId(). > I learned this style mostly from working with > http://gmaps-utility- library.googlecode.com/svn/trunk/markermanager/1.0/src/markermanager.js They are _not_ defining instance methods there that call prototype methods. Anyhow, take everything client-side from Google with a handful of salt, if at all. For example, all the outer `me's can be `this's -- /** * Removes all currently displayed markers * and calls resetManager to clear arrays */ MarkerManager.prototype.clearMarkers = function() { var me = this; me.processAll_(me.shownBounds_, me.removeOverlay_); me.resetManager_(); }; is particularly clueless. >> You are calling a prototype method from a method of an instance here >> when you should have called the prototype method *as* method of the >> instance instead (so that lookup along the prototype chain would have >> called that method automatically). > > I don't understand. Are you saying I should have written: > var getMyObjectId = myObject1.getId; > which "getMyObjectId();" will call MyObject.prototype.getId? No, of course you should have written var id = myObject1.getId(); >> > And if getId is defined under the Object prototype: In order to say, >> > specify the myObject.getId function as a event handler, you must make >> > a private member a function that calls the prototype method, or else >> > 'this' will be the global context. >> >> You use the terms without understanding their meaning. I don't even >> know where to begin correcting this nonsense. > > Sorry about using the wrong terms for what I mean, I will work on > improving my understanding of the correct terms. > By 'global context', I meant that 'this' would be of the Global > object. `this' could refer to the Global Object at most. And that would be bad style. PointedEars -- Prototype.js was written by people who don't know javascript for people who don't know javascript. People who don't know javascript are not the best source of advice on designing systems that use javascript. -- Richard Cornford, cljs, <f806at$ail$1$8300dec7(a)news.demon.co.uk>
From: nick on 1 Apr 2010 21:51
On Apr 1, 4:38 pm, Scott Sauyet <scott.sau...(a)gmail.com> wrote: > nick wrote: > > myApp.keys.register > > ('w', panNorth) > > ('s', panSouth) > > ('a', panWest) > > ('d', panEast); > > I agree with the other posters that it is in fact less clear. But > there are other options as well: Is it really so much less clear that you'd really spend any significant time wondering what that code did? I mean, I see what you guys are saying but I just don't see what else that code could possibly be doing. The name of the function appears once, and all the arg lists look the same, so I feel like it's pretty safe to assume (to someone reading chained code like this) that the same function is getting all the arg lists. > > myApp.keys.register({ > 'w': panNorth, > 's': panSouth, > 'a': panWest, > 'd': panEast > }); That example isn't too bad. The issue is that 'register' can take more parameters... another onRelease callback, and I'll probably add some kind of flags enum or something for modifier keys (still trying to decide how to expose that). So this: var keys = myApp.keys; keys.register ('w', panNorth, endPanNorth, keys.CTRL | keys.SHIFT) ('s', panSouth, endPanSouth, keys.CTRL | keys.SHIFT) ('a', panWest, endPanWest, keys.CTRL | keys.SHIFT) ('d', panEast, endPanEast, keys.CTRL | keys.SHIFT); ....Now becomes something like this: var keys = myApp.keys; keys.register({ 'w': [panNorth, endPanNorth, keys.CTRL | keys.SHIFT], 's': [panSouth, endPanSouth, keys.CTRL | keys.SHIFT], 'a': [panWest, endPanWest, keys.CTRL | keys.SHIFT], 'd': [panEast, endPanEast, keys.CTRL | keys.SHIFT] }); ....Which is still pretty decent I guess. You could even make the inner arrays into options so you could have named parameters. Luckily the way I am doing it now is flexible enough to incorporate something like this if I decide to do it later ;) > or > > myApp.keys.register( > 'w', panNorth, > 's', panSouth, > 'a', panWest, > 'd', panEast > ); It would be hard to do much with that given the optional args, I think. > > either of which feels a little more normal Javascript-y to me. > > -- Scott I'm sure you're right about that ;) |