Garrett: are the guy from ThoughtWorks? if so i read your bio i totally understand why you write the way you do. Garrett graduated with honors from Northwestern University with a bachelors of science in chemical and biological engineering concentrating in process control computer programming I studied chemistry so i think with this info we can understand each other better, avoid conflicts and become more participative than defensive.
On Dec 26, 2010, at 2:36 AM, Garrett Smith wrote: > On 12/25/10, fernando trasvina <trasv...@gmail.com> wrote: >> >> On Dec 24, 2010, at 8:44 PM, Garrett Smith wrote: >> >>> On 12/24/10, fernando trasvina <trasv...@gmail.com> wrote: >>>> >>>> On Dec 24, 2010, at 6:43 PM, Garrett Smith wrote: >>>> >>>>> On 12/24/10, Michael Haufe (TNO) <t...@thenewobjective.com> wrote: >>>>>> On Dec 24, 3:05 pm, Garrett Smith <dhtmlkitc...@gmail.com> wrote: >>>>>> >>>>>>> I rather have it one way or the other. e.g. >>>>>>> >>>>>>> makePoint(x, y); >>>>>>> >>>>>>> - OR - >>>>>>> >>>>>>> new Point(x, y); >>>>>>> >>>>>>> I just don't like seeing any extra if/else in the code. I also don't >>>>>>> want to handle the case where somebody might be relying on an anomaly >>>>>>> of calling the constructor as a function call. >>>>>> >>>>>> If defensive programming isn't necessary, of course. But since JS >>>>>> can't statically enforce such things it may be necessary to do so. >>>>>> >>>>> If a factory is used, then that's irrelevant. Toy example: >>>>> >>>>> function getAPoint(x, y) { >>>>> >>>>> } >>>>> The worst the client could do would be to use `new getAPoint`. That >>>>> would be a problem if the API expects `this` to be global object. >>>>> >>>>> Methods can be shared in scope, but the x and y properties can be >>>>> instance properties. >>>>> >>>>> function getAPoint(x, y) { >>>>> function distanceFromOrigin() { >>>>> return Math.sqrt((this.x * this.x) + (this.y * this.y)); >>>>> } >>>>> getAPoint = function(x, y) { >>>>> return { >>>>> x : x, >>>>> y : y, >>>>> distanceFromOrigin: distanceFromOrigin >>>>> }; >>>>> }; >>>>> return getAPoint(x, y); >>>>> } >>>>> getAPoint(4, 0).distanceFromOrigin(); >>>> >>>> I would say that coding this way should not be done unless there is an >>>> extreme requirement for it. >>>>> >>>>> The downside to that is `distanceFromOrigin` is hanging off the VO, so >>>>> it looks like a private static method, so what is `this`? >>>>> >>>>> It might be OK to "leak" a little implementation detail in this case: >>>>> >>>>> function getAPoint(x, y) { >>>>> function Point(x, y) { >>>>> this.x = +x; >>>>> this.y = +y; >>>>> } >>>>> Point.prototype = { >>>>> distanceFromOrigin : function() { >>>>> return Math.sqrt((this.x * this.x) + (this.y * this.y)); >>>>> } >>>>> }; >>>>> >>>>> getAPoint = function(x, y) { >>>>> return new Point(x, y); >>>>> }; >>>>> >>>>> return getAPoint(x, y); >>>>> } >>>> >>>> this should be done this way. you should not be defining the constructor >>>> function every time you run your factory, >>> >>> You're making a statement about the code that is false. The example >>> uses a technique that is known as "function rewriting" or "russian >>> doll" or I've explained it with more elaboration below. >>> >>> this what is >>>> really doing is creating a new constructor function every time and >>>> building >>>> an instance for it completely useless because because >>>> then how would you do instanceof? never put this type of closures for >>>> factories unless really needed. >>>> >>> >>> Your conclusion follows your analysis, which unfortunately is >>> incorrect. Please see my explanation below. >>> >>>> var Point = function(){}; >>>> >>>> var getAPoint = function(x,y){ >>>> return new Point(x,y); >>>> } >>>> >>> What's missing from that example? >> are you making up a point of a rhetorical question in here? nothing is >> missing, you can run the code and get an instance of Point >> > > The Point constructor doesn't really do anything. It looks like you > forgot something. And you obviously forgot a semicolon after > `getAPoint`. > > >> please run it and you will se that this is correct >> >> alert (getAPoint(1,2) instanceof Point); >> >> it is pointless to write the implementation of Point because it is not >> needed to probe that the resultant object is correct. >> > > Yeah, pointless. > >>> >>> Here is my explanation: >>>>> >>>>> The Point constructor is cached on the VO of the outer getAPoint. >>>>> Outer getAPoint identifier gets assigned to inner getAPoint identifier >>>>> but the scope chain of the inner getAPoint function has the Point >>>>> constructor and prototype. >> >> OMG lectures again? >> > > Well, if you don't want to learn it, then you don't have to. > >> just run your code > > Be sure, I run my code before posting. > >> >> function getAPoint(x, y) { >> function Point(x, y) { >> this.x = +x; >> this.y = +y; >> } >> Point.prototype = { >> distanceFromOrigin : function() { >> return Math.sqrt((this.x * this.x) + (this.y * this.y)); >> } >> }; >> >> getAPoint = function(x, y) { >> return new Point(x, y); >> }; >> >> return getAPoint(x, y); >> } >> > I ran that. > > I did not run the following, as it is not my code: >> console.log(getAPoint(1,2)) //Object { x=1, y=2} >> console.log(getAPoint(1,2) instanceof Point) //false >> > > That's your code not mine. And it does not do what you said it does. > That code should result ReferenceError, as Point would not be resolved > there. Well, unless you added a separate Point constructor and got > yourself confused while typing into FB. > >> please check that you so called function rewriting is accomplished by >> returning tthe execution of inner getPoint which results always in an >> instance of inner Point, which is useless for introspection. >> > > The inner Point? Again there's only one Point constructor in the example. > > [snip massive overquote with signatures] > -- > Garrett > > -- > To view archived discussions from the original JSMentors Mailman list: > http://www.mail-archive.com/jsmentors@jsmentors.com/ > > To search via a non-Google archive, visit here: > http://www.mail-archive.com/jsmentors@googlegroups.com/ > > To unsubscribe from this group, send email to > jsmentors+unsubscr...@googlegroups.com -- To view archived discussions from the original JSMentors Mailman list: http://www.mail-archive.com/jsmentors@jsmentors.com/ To search via a non-Google archive, visit here: http://www.mail-archive.com/jsmentors@googlegroups.com/ To unsubscribe from this group, send email to jsmentors+unsubscr...@googlegroups.com