Prev: FAQ Topic - Why does simple decimal arithmetic give strange results? (2010-03-31)
Next: Consolidate Credit Card Debt
From: Thomas Allen on 1 Apr 2010 11:38 On Apr 1, 6:56 am, Richard Cornford <Rich...(a)litotes.demon.co.uk> wrote: > On Apr 1, 12:10 am, nick wrote: > > To me something like > > this is easily understandable, if a bit esoteric: > > > Cufon('h1')('h2')('h3'); > > Looking at this code itself we see a call to (what is assumed to be) a > function, and then a call to whatever is returned from that first > call, and then a call to whatever is returned from that. We might also > observe a similarity in the arguments. > > That is all the information available in the code. In order to > determine that this actually represents three calls to the same > function it will be necessary to find the code for a - Cufon - > definition, or the code for whatever function is assigned to - Cufon - > at any given time. Otherwise (even if 'Cfon' were instead an > Identifier that described what the function did) the nature/process of > the second and third function calls remains unknown. It is not even > hinted at by the code above, and that is an objective fact. To be fair, I think that code intended for other developers' use should be well-documented. So, in my opinion, your concern about the "nature/process of the second and third function calls" is unwarranted, provided that the function's return value (and usage too, given the bizarre operation nick wants) is explained adequately. Thomas
From: Thomas 'PointedEars' Lahn on 1 Apr 2010 11:47 johnwlockwood wrote: > Asen Bozhilov wrote: > <snip> >> var me=this; >> me.register = function(){}; > > Isn't this done so you can reference the this from a closure function? > like: > function MyObject(id) > { > var me = this; > me.id = id; > me.getId_ = function() > { > return me.getId(); > }; > }; Unless you want to call the function as method of another object and keep the reference to the original object, there is no point in using `me' here. > var MyObject.prototype.getId = function() Syntax error, see below. > { > var me = this; > return me.id; > } > > var myObject = MyObject("test"); > > var getMyObjectId = myObject.getId_; You must not have tested that. > // or assign myObject.getId_ as a callback > > alert(getMyOjbectId()); > > // alert should display "test" Just because you wish for a thing that does not make it so. >> Why do you use that code? `this` associated with execution context >> immutable, and will be much more fast if you use `this` instead of >> identifier, which evaluate against Scope Chain. > > I was told there was a bug in one of the browsers that caused a > problem if you didn't use the me=this. You have been told nonsense. It's not a bug, it's a feature, and whether it matters at all depends on the context. > function MyObject(id) > { > this.id = id; > this.getId_ = function() > { > return this.getId(); > }; > }; 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 MyObject.prototype.getId = function() ^^^^^^^^^^^^^ > { > return this.id; > } > > This is obviously a very contrived example. More, it is syntactically invalid, and pointless if fixed. `MyObject.…' can never be produced by /Identifier/. (If you lost the `var', it would only be slightly less pointless, see above.) Maybe you were looking for function MyObject(id) { var _id = id; this.getId = function () { return _id; }; } PointedEars -- var bugRiddenCrashPronePieceOfJunk = ( navigator.userAgent.indexOf('MSIE 5') != -1 && navigator.userAgent.indexOf('Mac') != -1 ) // Plone, register_function.js:16
From: johnwlockwood on 1 Apr 2010 13:03 On Apr 1, 10:47 am, Thomas 'PointedEars' Lahn <PointedE...(a)web.de> wrote: > Unless you want to call the function as method of another object and keep > the reference to the original object, there is no point in using `me' here. Thank you for the clarification. > > function MyObject(id) > > { > > this.id = id; > > this.getId_ = function() > > { > > return this.getId(); > > }; > > }; > > 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 MyObject.prototype.getId = function() > ^^^^^^^^^^^^^ Okay, syntactically corrected: var 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; }; var myObject1; myObject1 = new MyObject("test"); // ^^^^ var getMyObjectId; getMyObjectId = myObject1.getId_; alert(getMyObjectId()); > > More, it is syntactically invalid, and pointless if fixed. `MyObject. ' > can never be produced by /Identifier/. (If you lost the `var', it would > only be slightly less pointless, see above.) Maybe you were looking for > > function MyObject(id) > { > var _id = id; > this.getId = function () { > return _id; > }; > } This does behave the same as my version, and it is more brief, but it is my understanding that defining methods of and object using prototype was a good thing. 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. -John
From: Asen Bozhilov on 1 Apr 2010 13:46 johnwlockwood wrote: > var alert; Why do you define variable in global execution context with Identifier `alert`? Your program works is questionable. Because you cannot be sure where is defined `alert`. Your program will work only if `alert` property is property of Global Object, because during variable instantiation for global code will be used Global Object as Variable Object. And by rules for variable declaration value of property `alert` isn't replaced if Global Object has that property already. > 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; > > }; > > var myObject1; > myObject1 = new MyObject("test"); > // ^^^^ > var getMyObjectId; > getMyObjectId = myObject1.getId_; > alert(getMyObjectId()); > 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. In handler function developer can do whatever he need. Your object interface shouldn't care about developer misconceptions. If he need to invoke some of your methods in handler, he will invoke in handler. Your method not need to know how will be invoked. For proper works of your method only need proper invocation from developer. It's enough. Regards.
From: Thomas 'PointedEars' Lahn on 1 Apr 2010 14:11
johnwlockwood wrote: > Thomas 'PointedEars' Lahn wrote: > > Thank you for the clarification. You are welcome. >> > function MyObject(id) >> > { >> > this.id = id; >> > this.getId_ = function() >> > { >> > return this.getId(); >> > }; >> > }; >> >> 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 MyObject.prototype.getId = function() >> ^^^^^^^^^^^^^ > > Okay, syntactically corrected: > var alert; You don't want to do that. > 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? > var myObject1; > myObject1 = new MyObject("test"); > // ^^^^ Your point being? Besides var myObject1 = new MyObject("test"); suffices. > var getMyObjectId; > getMyObjectId = myObject1.getId_; var getMyObjectId = myObject1.getId_; > alert(getMyObjectId()); That is precisely the exception to the rule that I have mentioned. You would be calling the function as method of the Global Object. >> More, it is syntactically invalid, and pointless if fixed. `MyObject.…' >> can never be produced by /Identifier/. (If you lost the `var', it would >> only be slightly less pointless, see above.) Maybe you were looking for >> >> function MyObject(id) >> { >> var _id = id; >> this.getId = function () { >> return _id; >> }; >> } > > This does behave the same as my version, No, it does not. > and it is more brief, Which is entirely besides the point. > 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. 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). > 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. PointedEars -- var bugRiddenCrashPronePieceOfJunk = ( navigator.userAgent.indexOf('MSIE 5') != -1 && navigator.userAgent.indexOf('Mac') != -1 ) // Plone, register_function.js:16 |