如何在JavaScript链中获得价值在

问题描述:

好奇怪的标题,我知道。但问题很简单。在一类我希望能够做这两件事情:如何在JavaScript链中获得价值在

invoice.getAmount(); // returns 1000

invoice.getAmount().asCurrency(); // returns $1000.00

我可以做任何,只是不知道如何让这两种工作。

我对现在的第二个想法:

getAmount() { 
this._temp = this.amount; 
return this; 
} 

asCurrency(){ 
    if(this._temp){ 
    return "$" + this._temp + ".00"; 
    } 
} 

那是什么,我真的有,但这个概念表示的丑陋副本...

任何想法?

感谢

+0

你应该原型功能asCurrency()添加到号的Javascript – fxlacroix

+3

类型...或者用返回对象的' .valueOf()方法返回普通数值,返回格式化字符串的.asCurrency()方法,如果你不想装饰内置的原型对象。 – Pointy

窍门是使用valueOf()方法。

class Invoice { 

    constructor(value) { 
    this.value = value; 
    } 

    getAmount() { 
    return { 
     valueOf: _ => this.value, 
     asCurrency: _ => '$' + this.value 
    } 
    } 
} 

const i = new Invoice(150); 

console.log(i.getAmount() + 10); // 160 
console.log(i.getAmount().asCurrency()); // '$150' 

记住getAmount返回一个数字。

因此,没有办法上的getAmount返回值链asCurrency(不使用valueOf),除非存在于Number原型asCurrency

如果你想保持这一切都在你的类组成,而无需修改Number原型,你需要或者使用valueOf(最佳解决方案),或者使getAmount您的类的实例您的返回值,以便您可以用asCurrency IT连锁。

您可以覆盖在Invoice.prototype几个内置插件(toString()valueOf()),像这样:

function Invoice(amount) { 
 
    this.amount = amount; 
 
} 
 

 
Invoice.prototype.toString = 
 
Invoice.prototype.valueOf = function valueOf() { 
 
    return this.value; 
 
}; 
 

 
Invoice.prototype.getAmount = function getAmount() { 
 
    this.value = this.amount; 
 
    
 
    return this; 
 
}; 
 

 
Invoice.prototype.asCurrency = function asCurrency() { 
 
    this.value = '$' + this.value.toFixed(2); 
 
    
 
    return this; 
 
}; 
 

 
var invoice = new Invoice(1000); 
 

 
console.log(Number(invoice.getAmount())); 
 
console.log(String(invoice.getAmount().asCurrency())); 
 

 
// or more subtly 
 
console.log(invoice.getAmount() + 0); 
 
console.log(invoice.getAmount().asCurrency() + '');

或者使用ES6 class

class Invoice { 
 
    constructor(amount) { 
 
    this.amount = amount; 
 
    } 
 
    
 
    toString() { 
 
    return this.value; 
 
    } 
 
    
 
    valueOf() { 
 
    return this.value; 
 
    } 
 
    
 
    getAmount() { 
 
    this.value = this.amount; 
 
    
 
    return this; 
 
    } 
 
    
 
    asCurrency() { 
 
    this.value = '$' + this.value.toFixed(2); 
 
    
 
    return this; 
 
    } 
 
} 
 

 
var invoice = new Invoice(1000); 
 

 
console.log(Number(invoice.getAmount())); 
 
console.log(String(invoice.getAmount().asCurrency())); 
 

 
// or more subtly 
 
console.log(invoice.getAmount() + 0); 
 
console.log(invoice.getAmount().asCurrency() + '');

+2

这看起来简单而优雅。让我试试看,如果它符合我的要求,我会标记你为答案。谢谢 –

+0

对不起,但我最终使用@ankr解决方案。你的要求我使用数字/字符串在我的返回值。 –

+0

@RomainGonçalvès我可能应该指出,他的解决方案也需要这样做,他只是含蓄地调用'valueOf()''+ 10' –

您可以使用Number.prototype.toLocaleString()

function getAmount() { 
 
    return 1000; 
 
} 
 

 
var result = getAmount().toLocaleString('en-EN', {style: 'currency', currency: 'USD'}); 
 

 
console.log(result);

+0

这是一个好主意。 – Pointy

+1

“我如何链接?” - “别”???这基本上就是你说的吗?因为我不明白这是如何解决这个问题的。 –

+0

@PatrickRoberts试试这个:'Number(1000).toLocaleString('en-EN',{style:'currency',currency:'USD'})' –