Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6200591
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T04:23:50+00:00 2026-05-24T04:23:50+00:00

Can someone help me understand the object oriented approach to javascript? I am used

  • 0

Can someone help me understand the object oriented approach to javascript? I am used to writing js code as follows:

function o_deal(id) {
    var hand = gcard1 + ", " + gcard2;
        var res = gcard1_val + gcard2_val;

    document.getElementById(id).innerHTML = hand;

    if (res == 21) {
        alert("Blackjack!");
    }
if (bucket == 0) {
    bucket = " ";
}

var card3_val = Math.floor(Math.random() * deck.length);
var nhand = deck[card3_val];
bucket = bucket + " " + nhand + ", ";
bucket_val = bucket_val + gcard1_val + gcard2_val + card3_val;

if (bucket_val >= 22) {
    var r = confirm("Bust! By " + nhand);
    if (r == true) {
        refresh();
    }
    else {
        refresh();
    }
}

document.getElementById(id).innerHTML = bucket;
}

But I’ve seen a number of posters on stack overflow that write code like this:

var Hand = function(bjcallback) {

    this.cards = [];

    this.onblackjack = bjcallback;

    this.deck = [1,2,3,4,5,6,7,8,9,10,"Jack","Queen","King","Ace"];

    this.values = {
        "Jack": 10,
        "Queen": 10,
        "King": 10,
        "Ace": 11
    };

    this.sum = function() {
        var i, x, res = 0;
        for (i in this.cards) {
            x = this.cards[i];
            if (typeof(x) != 'number') { x = this.values[x] };
            res += x;
        };
        return res
    };

    this.pick = function() {
        var pos = Math.floor(Math.random() * this.deck.length);
        var card = this.deck[pos];
        console.log(card);
        return card
    };

    this.deal = function(n) {
        n = n || 2;

Can someone please break the second method down so I can understand the difference? Any help would be appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T04:23:51+00:00Added an answer on May 24, 2026 at 4:23 am

    Object orientation in javascript is not two hard. You just bundle functionality and data together.

    So we have some functionality.

    I’ll just look at this snippet

    function o_deal(id) {
        var hand = gcard1 + ", " + gcard2,
            res = gcard1_val + gcard2_val;
    
        document.getElementById(id).innerHTML = hand;
    
        if (res == 21) {
            alert("Blackjack!");
        }
    }
    

    Let’s refactor this. We would need a few functions

    • isBlackjack. for checking whether we’ve won.
    • toString for rendering the hand

    Now we need to define hand.

    var Hand = {
       "isBlackjack": function() {
         return this.cards[0].value + this.cards[1].value === 21;
       },
       "toString": function() {
         return this.cards[0].toString() + " " + this.cards[1].toString();
       }
     }
    

    Now we can refactor o_deal to

    function o_deal(id, hand) {
        document.getElementById(id).innerHTML = hand.toString();
    
        if (hand.isBlackjack()) {
            alert("Blackjack!");
        }
    }
    

    Of course we still need the concepts of cards and we need to be able to make a hand.

    Making a hand is easy. var hand = Object.create(Hand)

    We also need a Card object which needs a toString method

    var CardList = [null, "1","2","3","4","5","6","7","8","9","X","A"];
    
    var Card = {
      "toString": function() {
        return CardList[this.value];
      }
    }
    

    Now we just need a way to create a hand

    var createHand = function() {
      var hand = Object.create(Hand);
      var card1 = Object.create(Card);
      var card2 = Object.create(Card);
      card1.value = Math.floor(Math.random() * 11);
      card2.value = Math.floor(Math.random() * 11);
      hand.cards = [card1, card2];
      return hand;
    }
    

    Now hopefully you can see how encapsulation and binding data and functionality together is useful.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can someone help me with the getopt function? When I do the following in
I am wondering if someone can help me figure out the best approach to
Can someone help me understand how best to model a composition relationship? If for
I know this regex divides a text into sentences. Can someone help me understand
Here is a little Javascript code snippet where I have used a function to
I got segmentation fault for the following code, could someone help me understand why?
Can someone help me identify what the purpose of this unidentified syntax is. It
Can someone help me figure out the following make file? BINS=file1 file2 file3 all:
Can someone help explain the following: If I type: a=`ls -l` Then the output
Can someone just help me refresh my mind? How do you specify hex values

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.