0

I want to implement data hiding in JavaScript by writing a function that returns an object from inside a closure. Here is the incorrect code:

pokerObjects.getPokerCard = (function(f, s){
        // These are immutable, so hide them in the closure
        var face = (typeof f === 'number' ? pokerObjects.faces[f] :  f);
        var suit = (typeof s === 'number' ? pokerObjects.suits[s] :  s);

        return {
            to_s: function() { return face + " of " + suit; }
          };

      })(f,s);

What I would like to do is be able to call the function getPokerCard with two parameters and have those parameters passed to the anonymous function defined parenthetically. But, passing them as written above gives me a ReferenceError: f is not defined when I try to parse the code.

2
  • 2
    Why do they "need to be passed"? Your question is a little unclear. Commented Dec 7, 2011 at 15:08
  • Explain where the parans f,s that you are passing to the anon function where they come from, and why can't you check face and suit every time, is this an final property or a method? It's unclear... Commented Dec 7, 2011 at 15:17

2 Answers 2

2

Maybe I'm not understanding your question correctly, but it seems like you want to assign a function to pokerObjects.getPokerCard which would allow you to later call pokerObjects.getPokerCard(f, s) which returns the object with getFace, getSuit, and to_s.

pokerObjects.getPokerCard = function(f, s) {
    // These are immutable, so hide them in the function.
    var face = (typeof f === 'number' ? pokerObjects.faces[f] :  f);
    var suit = (typeof s === 'number' ? pokerObjects.suits[s] :  s);

    return {
        getFace: function() { return face; },
        getSuit: function() { return suit; },
        to_s: function() { return face + " of " + suit; }
    };
};

This accomplishes the same thing, while still ensuring that face and suit remain hidden. They are scoped variables within the function.

Sign up to request clarification or add additional context in comments.

1 Comment

This "data hiding" doesn't hide any data and comes at significant run-time penalty. Practices like these should be avoided.
0
pokerObjects.getPokerCard = getPokerCard;

Card.prototype.toString = cardToString;

function getPokerCard(face, suit) {
  return new Card(
    typeof face === "number" ? this.faces[face] : face, 
    typeof suit === "number" ? this.suits[suit] : suit
  );
}

function Card(face, suit) {
  this.face = face;
  this.suit = suit;
}

function cardToString() {
  return this.face + " of " + this.suit;
}

There is no private in JavaScript. There is no point in data hiding if you expose getters for the data. Just use properties instead (make then non-writable if you really want)

2 Comments

This doesn't allow the data-hiding, which was the point of the original exercise. face and suit are properties of the card object, but should be defined inside the closure instead.
@Jekke why do you want data hiding, again you expose getters for it, so your not hiding anything. As an aside you can't have "data hiding" without severe run-time penalties, so avoid it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.