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 3 of 3 FirstFirst 123
Results 31 to 37 of 37
  1. #31
    Regular Coder
    Join Date
    Jan 2013
    Location
    Germany
    Posts
    578
    Thanks
    4
    Thanked 77 Times in 77 Posts
    My closure solution does not expose it's internals. There is no way of accessing the variable used inside other than calling the function, so I'm gonna have to disagree on "you will always have that with Javascript".

    Even if it were accessible through some hack, it'd still be better since you don't openly offer a way to directly affect the internals.

  2. Users who have thanked Airblader for this post:

    phantom007 (10-01-2013)

  3. #32
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,215
    Thanks
    12
    Thanked 338 Times in 334 Posts
    of course, you can wrap everything private in a Closure ...
    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

  4. #33
    Regular Coder
    Join Date
    Jan 2013
    Location
    Germany
    Posts
    578
    Thanks
    4
    Thanked 77 Times in 77 Posts
    No, you can't. You can't wrap your solution to both expose and hide the internals because it is just based on the idea of exposing it.
    And if you were to wrap your solution, you'd basically get my solution and you'd no longer have an actual reason to attach the information as a property instead of using a local variable.

  5. #34
    Regular Coder
    Join Date
    Jan 2013
    Location
    Germany
    Posts
    578
    Thanks
    4
    Thanked 77 Times in 77 Posts
    To be a little fair: While my solution is better in the sense of encapsulation, one could argue that closure can be potential memory leaks, though it's more of a general thing rather than specific to this solution since it doesn't apply here.

    In any case, your solution is certainly more beginner-friendly when it comes to understanding it.

  6. #35
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,215
    Thanks
    12
    Thanked 338 Times in 334 Posts
    Quote Originally Posted by Airblader View Post
    In any case, your solution is certainly more beginner-friendly when it comes to understanding it.
    that’s why I added it to the discussion.

    (and to be honest, how many beginners do understand the importance of encapsulation?)
    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. #36
    Regular Coder
    Join Date
    Jan 2013
    Location
    Germany
    Posts
    578
    Thanks
    4
    Thanked 77 Times in 77 Posts
    Quote Originally Posted by Dormilich View Post
    (and to be honest, how many beginners do understand the importance of encapsulation?)
    When I wrote my answer I knew he was a beginner, I didn't know he hadn't heard of any of common Javascript concepts.

    I usually try to go with good code, even if it's beginners. Why? Because they may not realize the importance of the involved concepts (or even the concepts at all if I don't point them out), but they certainly will remember what it looks like. Once you show a beginner document.write, he is likely to use it from that point on.

    I also didn't even explain the concept until he specifically asked for it. Kudos to the OP, by the way, for being one of the few out there actually asking questions and wanting to understand. Don't see that too often, but it's the kind of people I like to help the most (and where I don't mind writing lengthy posts).

  8. #37
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,215
    Thanks
    12
    Thanked 338 Times in 334 Posts
    some more ideas how to tackle the problem:
    PHP Code:
    // create an object with a property that is set on load
    // and persists with that value throughout the script lifetime
    var randomNumber = {};
    Object.defineProperty(randomNumber"value", {
        
    valueMath.floor(Math.random() * 1),
        
    writablefalse
    });
    // use
    var fixedRandom randomNumber.value;
    // fails
    randomNumber.value null;

    // if you need more of that, use a Factory
    function randomNumberGenerator()
    {
        var 
    randomNumber = {};
        
    Object.defineProperty(randomNumber"value", {
            
    valueMath.floor(Math.random() * 1),
            
    writablefalse
        
    });
        return 
    randomNumber;
    }
    // use
    var rand1 randomNumberGenerator();
    var 
    randNum rand1.value;

    // you can do that with a function as well
    function fixedRandomNumber()
    {
        return 
    fixedRandomNumber.value || 0;
    }
    Object.defineProperty(fixedRandomNumber"value", {
        
    valueMath.floor(Math.random() * 1),
        
    writablefalse
    });
    // use
    var rand1 fixedRandomNumber(); 
    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


 
Page 3 of 3 FirstFirst 123

Posting Permissions

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