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

  • SEARCH
  • Home
  • 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 8048949
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T06:38:32+00:00 2026-06-05T06:38:32+00:00

So lets say I want to make a simple Price class in JS. It’s

  • 0

So lets say I want to make a simple Price class in JS. It’s basically a number so I thought I would just inherit from a Number. Here is some code:

Number.prototype.toMoney = function (precision, decimals, thousands) {
    // Formats number...
}

function Price(val) {
    Number.call(val); // Based on MozillaDN
}

Price.sufix = ' EUR'; // To print with every Price

// Price.prototype = Number.prototype;
Price.prototype = new Number(); // any difference?

Price.prototype.toString = function() {
    return this.toMoney() + Price.sufix; // Of course it does not work.
}

var price = new Price(100.50);
alert(price.toString()); // Gives: undefined EUR
alert(price); // This fail. I thought it should work as it works with Numbers.

I probably do something wrong, but I can’t find out what.

  • 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-06-05T06:38:34+00:00Added an answer on June 5, 2026 at 6:38 am

    You have a typo in this line I think:

    Price.prototype - new Number(); // any difference?
    

    You are assigning a new Number to be the prototype of Price so you need to use an = sign not a minus sign.

    It should read:

    Price.prototype = new Number(); // any difference?
    

    Having said that, it still doesn’t work. I can’t understand why you are attempting to inherit from Number.

    This works:

    function Price(val) {
        this.val = val;                
    }
    Price.suffix = ' EUR'; // To print with every Price
    
    Price.prototype = {
        toString: function() {
            return this.toMoney() + Price.suffix;
        },
        toMoney: function(precision, decimals, thousands){
            //fancy code here to format the number
            return "formatted number " + this.val;
        }
    };
    
    var price = new Price(100.50);
    alert(price.toString()); // returns: formatted number 100.50 EUR
    alert(price); 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Lets say I want to make a class myClass with two slots A and
Lets say I want to make few classes to determine behaviour of agents. The
Let's say I want to make a simple project tracking system. A manager can
Let's say I want to make a HumanBody class. And I want to store
Trying to make something simple in JS. Lets say I have two images, I
I have two vectors with different dimensions. To make it simple,lets say A =
Let's say I want make some of my sources publicly available via my blog
Let's say I want to make a little wrapper along the lines of: def
Let's say I have thousands of users and I want to make the passwords
Let's say I know two points that make up a line. I want to

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.