Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 2 of 3 FirstFirst 123 LastLast
Results 16 to 30 of 37
  1. #16
    Regular Coder
    Join Date
    Jan 2013
    Location
    Germany
    Posts
    578
    Thanks
    4
    Thanked 77 Times in 77 Posts
    Quote Originally Posted by cancer10 View Post
    Yes, I did that deliberately since the obj var is declared inside a function and the obj var needs to be accessed from a diff function which is why I had to declare it as global.
    No! Still don't do it. Like I said: Never declare variables as global. Ever! There is always a better way to do it!

    Global variables don't just let you access them from one scope level up, but from every scope. That's just really, really bad. You could let the one function return obj and then use that in the second function. Or you could wrap both functions in another scope and declare the variable there. But leaving out "var" is always, and I mean always, the worst and really bad choice. And any code-quality tool (JSLint, JSHint, …) will report an error for it.
    Last edited by Airblader; 09-29-2013 at 11:05 AM.

  2. Users who have thanked Airblader for this post:

    phantom007 (09-29-2013)

  3. #17
    Regular Coder
    Join Date
    Jun 2006
    Location
    UK
    Posts
    911
    Thanks
    302
    Thanked 2 Times in 2 Posts
    okay I got you, wil fix it in my code now.

    One more question,

    does the () at the end of your funtion serves a special purpose? I am asking coz I have seen the source code of jQuery and they are passing (window) in the brackets.

    Any idea why?

    Thanks

  4. #18
    Regular Coder
    Join Date
    Jan 2013
    Location
    Germany
    Posts
    578
    Thanks
    4
    Thanked 77 Times in 77 Posts
    Again, the outer function is a normal function – that you immediately call. Think of it this way:

    Code:
    function outerFunction () {
        return function () { /* ... */ };
    }
    
    var result = outerFunction();
    The only difference is that there is no need to create the name "outerFunction" if it is only used once anyway, so we just inline the function definition. The only thing we have to do is wrap the function body in parentheses, so we first do

    Code:
    // same as above
    
    var result = (outerFunction)();
    and then finally inline it as

    Code:
    var result = (function () { ... })()

    As you noticed, jQuery actually passes values into the outer(!) function. It actually does something like this

    Code:
    (function (window, undefined) {
        // ...
    })(window);
    There are two reasons why jQuery does that.

    The first one only affects the "undefined" parameter. As you know, undefined is a keyword in Javascript. However, some browsers allow overwriting the value of undefined. Of course it is a horrible thing to do, but you never know what other people do in their poorly written scripts (just like using globals ).
    Since jQuery is a heavily-used library with millions of users, they protect their own library against other code's mistakes by declaring "undefined" as an argument, but then not passing anything into the function. This forces the argument "undefined" to actually be undefined – so it doesn't matter if anyone else changed this value before!
    Of course, if you think about it, this means that jQuery itself overwrites the undefined value. But it actually only does it locally for itself, so even if someone else's code relies on undefined having a different value (again, this is terrible), their code won't break.

    In short: It is a very neat way of making sure that undefined has the correct value within the jQuery library.

    Now, why would they pass the window object? jQuery tries to be a library that is as small as it could possibly can when the code is minified. "window" is a built-in global with a fixed name and if jQuery were to use it all the time, it could not be minified. But by doing

    Code:
    (function (window) {})(window);
    they create an alias for the window object inside the function. When the code is minified, it will actually only look like this

    Code:
    (function(w){})(window);
    And, of course, any reference to "window" inside the function will accordingly be replaced with "w", making the code a lot shorter (if it is used a lot). The same goes for "undefined", so for this one there are actually two reasons (safety and aliasing).
    Last edited by Airblader; 09-29-2013 at 11:29 AM.

  5. Users who have thanked Airblader for this post:

    phantom007 (09-29-2013)

  6. #19
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,245
    Thanks
    12
    Thanked 340 Times in 336 Posts
    just to throw in a another new and confusing concept: memoizing
    PHP Code:
    function generateRandomNumber() {
        if (
    typeof generateRandomNumber.randomNumber !== 'number') {
            
    generateRandomNumber.randomNumber Math.floor(Math.random() * 1);
        }
        return 
    generateRandomNumber.randomNumber;

    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  7. Users who have thanked Dormilich for this post:

    phantom007 (09-30-2013)

  8. #20
    Regular Coder
    Join Date
    Jun 2006
    Location
    UK
    Posts
    911
    Thanks
    302
    Thanked 2 Times in 2 Posts

    Question

    Quote Originally Posted by Dormilich View Post
    just to throw in a another new and confusing concept: memoizing
    PHP Code:
    function generateRandomNumber() {
        if (
    typeof generateRandomNumber.randomNumber !== 'number') {
            
    generateRandomNumber.randomNumber Math.floor(Math.random() * 1);
        }
        return 
    generateRandomNumber.randomNumber;

    Hi

    Nice concept, but does not seem to work, pls check this example, also let me know how do I force it to generate new random number?

    http://jsbin.com/uMUhoJU/12/edit


    Thanks

  9. #21
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,245
    Thanks
    12
    Thanked 340 Times in 336 Posts
    works for me, though. becomes obvious if you alert the value when you click on the button.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  10. Users who have thanked Dormilich for this post:

    phantom007 (09-30-2013)

  11. #22
    Regular Coder
    Join Date
    Jun 2006
    Location
    UK
    Posts
    911
    Thanks
    302
    Thanked 2 Times in 2 Posts
    I am doing a console.log()

    pls check this link http://jsbin.com/uMUhoJU/12/edit?html,js,console,output

  12. #23
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,245
    Thanks
    12
    Thanked 340 Times in 336 Posts
    and that runs exactly once.

    just call the function in the console directly and you see it being constant.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  13. Users who have thanked Dormilich for this post:

    phantom007 (09-30-2013)

  14. #24
    Regular Coder
    Join Date
    Jun 2006
    Location
    UK
    Posts
    911
    Thanks
    302
    Thanked 2 Times in 2 Posts
    Yes I understand, but what I was looking for it to return the same value everytime unless I specify a true flag in the param.

  15. #25
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,245
    Thanks
    12
    Thanked 340 Times in 336 Posts
    Code:
    generateRandomNumber.randomNumber = null;
    resets the condition to create a new number.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  16. Users who have thanked Dormilich for this post:

    phantom007 (09-30-2013)

  17. #26
    Regular Coder
    Join Date
    Jun 2006
    Location
    UK
    Posts
    911
    Thanks
    302
    Thanked 2 Times in 2 Posts
    This is what I am doing http://jsbin.com/uMUhoJU/16/edit

  18. #27
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,245
    Thanks
    12
    Thanked 340 Times in 336 Posts
    defeats the purpose of memoizing. use
    Code:
    function generateRandomNumber() {
        if (typeof generateRandomNumber.randomNumber !== 'number') {
            generateRandomNumber.randomNumber = Math.floor(Math.random() * 9 + 1);
        }
        return generateRandomNumber.randomNumber;
    } 
    console.log(generateRandomNumber());
    console.log(generateRandomNumber());
    generateRandomNumber.randomNumber = null;
    console.log(generateRandomNumber());
    console.log(generateRandomNumber());
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  19. Users who have thanked Dormilich for this post:

    phantom007 (09-30-2013)

  20. #28
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,245
    Thanks
    12
    Thanked 340 Times in 336 Posts
    PS. calling the script section repeatedly is like reloading the page.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  21. Users who have thanked Dormilich for this post:

    phantom007 (09-30-2013)

  22. #29
    Regular Coder
    Join Date
    Jan 2013
    Location
    Germany
    Posts
    578
    Thanks
    4
    Thanked 77 Times in 77 Posts
    What I don't like about this approach is that it breaks encapsulation. It exposes the internals to the outside. Other than that, it's completely fine, of course.

  23. Users who have thanked Airblader for this post:

    phantom007 (10-01-2013)

  24. #30
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,245
    Thanks
    12
    Thanked 340 Times in 336 Posts
    Quote Originally Posted by Airblader View Post
    What I don't like about this approach is that it breaks encapsulation. It exposes the internals to the outside.
    you will always have that with JavaScript.

    though I am more worried about the use of global variables than that.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  25. Users who have thanked Dormilich for this post:

    phantom007 (10-01-2013)


 
Page 2 of 3 FirstFirst 123 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •