Hiya, > ...wouldn't "element" always refer to a node in > the DOM already? Is there a case where the method could fail if that > first line is not included?
The functions in the Element class are available both as class methods and as instance methods. What you're defining is the class method, which automagically gets "methodized" and applied to either the HTMLElement prototype (on most browsers) or specific elements when they're extended (on IE). More here: http://prototypejs.org/learn/extensions So answering your second question above, if someone uses the class version of the method and passes in an ID, the call to $() is necessary. Silly example: Element.addMethods({ showError: function(element, err) { element = $(element); element.update('<span class="error">' + err + '</span>'); } }); ...which can be called like this: Element.showError('target', 'Something failed.'); ...in which case the $() call is required. You're quite right that it's redundant if they use the methodized version: $('target').showError('Something failed'); ...since the element passed into it will already be both looked up and extended before it gets called. So I think it's the class method scenario that's the main reason you need it. HTH, -- T.J. Crowder tj / crowder software / com Independent Software Engineer, consulting services available On Jun 20, 2:00 am, barunio <baru...@gmail.com> wrote: > I'm trying to understand something about this example from the API > docs re: Element.addMethods: > > Element.addMethods({ > wrap: function(element, tagName) { > element = $(element); > var wrapper = document.createElement('tagName'); > element.parentNode.replaceChild(wrapper, element); > wrapper.appendChild(element); > return Element.extend(wrapper); > } > > }); > > Why does the "wrap" function include the line "element = $(element);"? > > In general, this line adds extra flexibility to a function. But in > this case the function is defined in the context of > Element.addMethods, so wouldn't "element" always refer to a node in > the DOM already? Is there a case where the method could fail if that > first line is not included? > > Thanks.. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Prototype & script.aculo.us" group. To post to this group, send email to prototype-scriptaculous@googlegroups.com To unsubscribe from this group, send email to prototype-scriptaculous+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/prototype-scriptaculous?hl=en -~----------~----~----~----~------~----~------~--~---