所以假设我想在 JS 中创建一个简单的 Price 类。它基本上是一个数字,所以我想我会从一个数字继承。这是一些代码:
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.
我可能做错了什么,但我不知道是什么。