Deal of The Day! Hurry Up, Grab the Special Discount - Save 25% - Ends In 00:00:00 Coupon code: SAVE25
Welcome to Pass4Success

- Free Preparation Discussions

Salesforce JS-Dev-101 Exam - Topic 6 Question 4 Discussion

A class was written to represent items for purchase in an online store, and a second class representing items that are on sale at a discounted price. The constructor sets the name to the first value passed in. There is a new requirement for a developer to implement a description method that will return a brief description for Item and SaleItem.01 let regItem = new Item('Scarf', 55);02 let saleItem = new SaleItem('Shirt', 80, .1);03 Item.prototype.description = function() { return 'This is a ' + this.name; }04 console.log(regItem.description());05 console.log(saleItem.description());0607 SaleItem.prototype.description = function() { return 'This is a discounted ' + this.name; }What is the output when executing the code above?
B) This is a Scarf This is a Shirt This is a Scarf This is a discounted Shirt
A) This is a Scarf Uncaught TypeError: saleItem.description is not a function This is a Scarf This is a discounted Shirt
C) This is a Scarf This is a Shirt This is a discounted Scarf This is a discounted Shirt
D) This is a Scarf Uncaught TypeError: saleItem.description is not a function This is a Shirt This is a discounted Shirt

Salesforce JS-Dev-101 Exam - Topic 6 Question 4 Discussion

Actual exam question for Salesforce's JS-Dev-101 exam
Question #: 4
Topic #: 6
[All JS-Dev-101 Questions]

A class was written to represent items for purchase in an online store, and a second class representing items that are on sale at a discounted price. The constructor sets the name to the first value passed in. There is a new requirement for a developer to implement a description method that will return a brief description for Item and SaleItem.

01 let regItem = new Item('Scarf', 55);

02 let saleItem = new SaleItem('Shirt', 80, .1);

03 Item.prototype.description = function() { return 'This is a ' + this.name; }

04 console.log(regItem.description());

05 console.log(saleItem.description());

06

07 SaleItem.prototype.description = function() { return 'This is a discounted ' + this.name; }

What is the output when executing the code above?

Show Suggested Answer Hide Answer
Suggested Answer: B

Comprehensive and Detailed

Assuming SaleItem inherits from Item via prototype (e.g. SaleItem.prototype = Object.create(Item.prototype)):

Lines 01--02: create regItem and saleItem.

Line 03: define Item.prototype.description.

Now both regItem and saleItem (via inheritance) have a description method from Item.prototype.

Line 04: regItem.description() 'This is a Scarf'.

Line 05: saleItem.description() 'This is a Shirt' (same method, but this.name is 'Shirt').

Line 07: SaleItem.prototype.description = ... overrides description only for SaleItem instances going forward.

If we imagine calling regItem.description() and saleItem.description() again after line 07:

regItem.description() still uses Item.prototype.description 'This is a Scarf'.

saleItem.description() now uses SaleItem.prototype.description 'This is a discounted Shirt'.

Those four lines correspond to option B.

________________________________________


Contribute your Thoughts:

0/2000 characters
Fredric
3 days ago
C seems right. SaleItem should inherit from Item.
upvoted 0 times
...
Gretchen
8 days ago
I feel like it's B. Both descriptions should work.
upvoted 0 times
...
Keena
13 days ago
I think the answer is A. The TypeError makes sense.
upvoted 0 times
...
Nu
18 days ago
But don't forget, the regular item description works fine.
upvoted 0 times
...
Osvaldo
24 days ago
So, option A seems right. It matches the error.
upvoted 0 times
...
Yuki
29 days ago
Yeah, I agree. The method isn't defined for SaleItem initially.
upvoted 0 times
...
Stephen
1 month ago
I think the output will show an error for saleItem.
upvoted 0 times
...
Vanna
1 month ago
But what if the SaleItem doesn't have a description method?
upvoted 0 times
...
Felicidad
1 month ago
I agree, it should work if the prototype is set up right.
upvoted 0 times
...
Junita
2 months ago
Wait, why would it throw a TypeError?
upvoted 0 times
...
Merilyn
2 months ago
I think option A is correct!
upvoted 0 times
...
Regenia
2 months ago
The SaleItem class needs to inherit from Item.
upvoted 0 times
...
Bok
2 months ago
I disagree, I think option D makes more sense.
upvoted 0 times
...
Mari
2 months ago
Definitely option B, both descriptions should work fine.
upvoted 0 times
...
Dick
4 months ago
Wait, why would there be a TypeError? That seems odd.
upvoted 0 times
...
Bulah
4 months ago
I think the output will be option A.
upvoted 0 times
...
Tomoko
4 months ago
The SaleItem class needs to inherit from Item for the method to work.
upvoted 0 times
...
Joesph
4 months ago
I thought both descriptions would work fine!
upvoted 0 times
...
Selma
5 months ago
Wait, why would there be a TypeError?
upvoted 0 times
...
Melita
5 months ago
Definitely going with option A!
upvoted 0 times
...
Sabine
5 months ago
I think the output will have an error for saleItem.
upvoted 0 times
...
Keneth
5 months ago
The SaleItem class needs to inherit from Item.
upvoted 0 times
...
Johana
5 months ago
I recall that if the SaleItem doesn't have its own description method defined properly, it might throw an error. That makes me lean towards option A.
upvoted 0 times
...
Venita
5 months ago
I think the first part of the output will definitely be "This is a Scarf," but I'm confused about what happens with the SaleItem.
upvoted 0 times
...
Kerry
6 months ago
I'm not entirely sure, but I feel like the SaleItem might not inherit the description method correctly. That could lead to an error.
upvoted 0 times
...
Rickie
6 months ago
I remember we practiced something similar where we had to define methods for different classes. I think the output should include both descriptions.
upvoted 0 times
...

Save Cancel