Question on Object oriented Programming in JS

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • QQ

    Question on Object oriented Programming in JS

    Hi All

    When I looked at ExtJS source code, I found something strange.

    could anyone here explain to me?

    In ext-base.js file
    they wrap the all code inside:

    (function() {
    ....
    ....
    ....

    )()


    What is that mean?

    Thanks
    Steven
  • Stevo

    #2
    Re: Question on Object oriented Programming in JS

    QQ wrote:
    could anyone here explain to me?
    they wrap the all code inside:
    >
    (function() {
    ...
    ...
    ...
    >
    )()
    >
    What is that mean?
    It's declaring an anonymous function (inside the outer parentheses) and
    then calling it (via the second pair of parentheses). The idea is to be
    able to run a block of code as if it was running inline, but having the
    advantage of running all that code in the namespace of the function and
    therefore not polluting/corrupting the global namespace. So it can
    declare lots of variables named for example, a, i, x and it can be sure
    that it's not overwriting any global a, i or x variables. If that code
    were just running inline, it would have to ensure it didn't clash with
    any global variable names, and it would leave all those variables behind.

    Comment

    • Joost Diepenmaat

      #3
      Re: Question on Object oriented Programming in JS

      QQ <chaojiang.au@g mail.comwrites:
      In ext-base.js file
      they wrap the all code inside:
      >
      (function() {
      ...
      ...
      ...
      >
      )()
      >
      >
      What is that mean?
      That means: create a function expression and then immediately execute
      it. The main reason to do that is to limit the scope of variables that
      shouldn't be global.

      (function() {
      var a =1;
      // a is defined here
      })();
      // a isn't defined here

      Nothing to do with OO.


      --
      Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/

      Comment

      • Jorge

        #4
        Re: Question on Object oriented Programming in JS

        On Jul 7, 1:00 pm, QQ <chaojiang...@g mail.comwrote:
        Hi All
        >
        When I looked at ExtJS source code, I found something strange.
        >
        could anyone here explain to me?
        >
        In ext-base.js file
        they wrap the all code inside:
        >
        (function() {
        ...
        ...
        ...
        >
        )()
        >
        What is that mean?
        >
        ( expression )( parameters )

        Evaluate "expression " and (try to) call the result as a function with
        the parameters "parameters ".

        "expression " does not need to be a "literal function" expression.

        for example :

        var p= "Hola";
        var f= function (p) { alert(p) };
        var g= function () { return f };
        var h= [f, g];

        (f)(p);
        (g())(p);
        (h[0])(p);
        (h[1]())(p);
        (window.alert)( p);
        (function (q) { alert(q) })(p);



        --Jorge.

        Comment

        • Jorge

          #5
          Re: Question on Object oriented Programming in JS

          On Jul 7, 3:47 pm, Jorge <jo...@jorgecha morro.comwrote:
          On Jul 7, 1:00 pm, QQ <chaojiang...@g mail.comwrote:
          >
          >
          >
          >
          >
          Hi All
          >
          When I looked at ExtJS source code, I found something strange.
          >
          could anyone here explain to me?
          >
          In ext-base.js file
          they wrap the all code inside:
          >
          (function() {
          ...
          ...
          ...
          >
          )()
          >
          What is that mean?
          >
          ( expression )( parameters )
          >
          Evaluate "expression " and (try to) call the result as a function with
          the parameters "parameters ".
          >
          "expression " does not need to be a "literal function" expression.
          >
          for example :
          >
          var p= "Hola";
          var f= function (p) { alert(p) };
          var g= function () { return f };
          var h= [f, g];
          >
          (f)(p);
          (g())(p);
          (h[0])(p);
          (h[1]())(p);
          (window.alert)( p);
          (function (q) { alert(q) })(p);
          >

          >
          Note that an assignment has a result as well :

          var i;

          (i= f)(p);
          ((i= h[1])())(p);

          --Jorge.

          Comment

          • Jorge

            #6
            Re: Question on Object oriented Programming in JS

            On Jul 7, 3:47 pm, Jorge <jo...@jorgecha morro.comwrote:
            On Jul 7, 1:00 pm, QQ <chaojiang...@g mail.comwrote:
            >
            >
            >
            >
            >
            Hi All
            >
            When I looked at ExtJS source code, I found something strange.
            >
            could anyone here explain to me?
            >
            In ext-base.js file
            they wrap the all code inside:
            >
            (function() {
            ...
            ...
            ...
            >
            )()
            >
            What is that mean?
            >
            ( expression )( parameters )
            >
            Evaluate "expression " and (try to) call the result as a function with
            the parameters "parameters ".
            >
            "expression " does not need to be a "literal function" expression.
            >
            for example :
            >
            var p= "Hola";
            var f= function (p) { alert(p) };
            var g= function () { return f };
            var h= [f, g];
            >
            (f)(p);
            (g())(p);
            (h[0])(p);
            (h[1]())(p);
            (window.alert)( p);
            (function (q) { alert(q) })(p);
            >

            >
            Note that an assignment yields a result as well :

            var i, j;

            (i= f)(p);
            (j= (i= h[1])())(p);
            (j)(p);
            (i())(p);

            --Jorge.

            Comment

            Working...