myFunction()
and
window.myFunction

are not necessarily the same. the first one will look up the scope
chain and use the first ocurrence, it doesn't go directly to the
window object.

var x = 3;
(function(){
  var x = 5;
  alert(x);
})();
alert(x);

Again, this is basic javascript, I'll stop here. There are plenty of
resources on this matter around, including a lenghty discussion here
on the list, Brian should take a look at that.

cheers
-- ricardo

On May 30, 1:25 pm, MorningZ <morni...@gmail.com> wrote:
> No, because if you say
>
> <script type="text/javascript">
> function MyFunctionThatAdds(x, y) {
>   ..........}
>
> </script>
>
> that *technically* attaches that function to the "window" object
>
> so saying
>
> var sum = MyFunctionThatAdds(3, 4);
> alert(sum);
>
> is the same as saying
>
> var sum = window.MyFunctionThatAdds(3, 4);
> alert(sum);
>
> which is also the same as
>
> window.sum = window.MyFunctionThatAdds(3, 4);
> alert(window.sum);
>
> Working example (all 3 will result in "7"):http://paste.pocoo.org/show/119909/
>
> So keeping that in mind, saying:
>
> var MZ = {};
> MZ.MyFunction = function(x, y) {
>     return x + 7;
>
> }
>
> i get the same results for:
>
> alert(MZ.MyFunction(3,4));
> and
> alert(window.MZ.MyFunction(3,4));
>
> as for "global functions are bad", i would agree with that if they are
> used unnecessarily and in excess, but sometimes one wants common
> functionality no matter where they are in the code
>
> On May 30, 10:49 am, waseem sabjee <waseemsab...@gmail.com> wrote:
>
> > so is namespace faking bad ?
>
> > On Sat, May 30, 2009 at 4:19 PM, Ricardo <ricardob...@gmail.com> wrote:
>
> > > Because you're creating a global variable, and global vars are evil :)
> > > Same reason why you don't want to pollute the jQuery namespace.
>
> > > On May 30, 1:27 am, MorningZ <morni...@gmail.com> wrote:
> > > > "If i make a custom function my site uses a lot. i put it in a
> > > > plugin. :) "
>
> > > > Why not fake a namespace instead?
>
> > > > like if you had
>
> > > > function toUpperCase() {
> > > >    //your custom "to upper case" code that
> > > >    //would conflict with JS's verion
>
> > > > }
>
> > > > simply saying
>
> > > > var waseem = {};
> > > > waseem.toUpperCase = function() {
> > > >    //your custom "to upper case" code that
> > > >    //would no longerconflict with JS's verion
>
> > > > }
>
> > > > would solve the issue....
>
> > > > back on topic, i haven't seen anyone suggest that the original code
>
> > > > $(function(){
> > > >    function displayMessage(){ alert('hello world'); };});
>
> > > > $(function(){
> > > >   displayMessage();
>
> > > > });
>
> > > > be written like
>
> > > > function displayMessage(){ alert('hello world'); };
>
> > > > $(function(){
> > > >   displayMessage();});
>
> > > > $(function(){
> > > >   displayMessage();
>
> > > > }
> > > > });
>
> > > > which makes a lot of sense, and is a super simple change

Reply via email to