Question

Javascript Provide code to create a custom object named pokerCard containing the suit property with a...

Javascript

Provide code to create a custom object named pokerCard containing the suit property with a value of "Spades" and a rank property with a value of "King".

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Solution:

We can create a custom object in following two ways:

1. Creating it as an object literal:

A custom object can be created by an object literal by storing the properties of the object within a comma-separated list of name:value pairs enclosed within a set of curly braces.

The general syntax of the object literal is

        var objName = { name1: value1, name2: value2,
            ...
        };

where objName is the name of the object, name1, name2, and so on are the names associated with that object, and value1, value2, and so on are the values assigned to those names.

Code to create a custom object named pokerCard containing the suit property with a value of "Spades" and a rank property with a value of "King":

var card1 = { title: "pokerCard", suit: "Spades", rank: King};

2. Using an object constructor:

To create such object classes, you apply a function known as an object constructor or a constructor that defines the properties and methods associated with the object type. A specific object that is based on an object class is known as an object instance or instance. Creating an object instance based on an object class is known as instantiating an object.

Code to create a custom object named pokerCard containing the suit property with a value of "Spades" and a rank property with a value of "King":

The constructor function for an object class of poker cards might appear as follows:

function pokerCard(cardSuit, cardRank) {
        this.suit = cardSuit;
        this.rank = cardRank;
        this.rankValue = null;
        this.showCard() function() {
            return "Your card is a " + this.rank + " of " + this.suit;
        }
    }

Once the constructor function for the object class is defined, instances of the object are created with the command

    var objInstance = new objClass(parameters);

where objInstance is a specific instance of the object, objClass is the object class as defined by the constructor function, and parameters are the values of any parameters included in the constructor function. Thus, the following code creates two instances of the class of pokerCard objects, one for the king of hearts and the other for the seven of spades:

    var card1 = new pokerCard("Spades", "King");
                card1.rankValue = 13;

Please give thumbsup, if you like it. Thanks.

Add a comment
Know the answer?
Add Answer to:
Javascript Provide code to create a custom object named pokerCard containing the suit property with a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT