好的,所以我试图想出一个很好的例子,所以我创建了一个关于贵金属的构造函数。构造函数采用金属和重量类型。我有两种方法。一种方法确定贵金属(金或银)是否真实,另一种方法根据现货价格计算价值。 (我知道现货价格是错误的,这只是一个例子)。
假设一位顾客带来了一块80%银的银片。因为它的80%银我想把它应用到我的metalValue方法。我该怎么做。
这是代码。 (JSFiddle为您提供了便利http://jsfiddle.net/bwj3fv12/)。 这将有助于我更好地理解构造函数。
HTML
<div id="testDiv">test Div</div>
<div id="testDiv2">test Div2</div> <br /><br />
的JavaScript
var PreciousMetals = function(metal, weight){
this.metal = metal;
this.weight = weight; //weight in ounces
this.authentic = function(colorTest){
var metalPurity;
var zero = "";
if (this.metal == "silver"){
switch(colorTest){
case "brightred":
metalPurity = 1;
break;
case "darkred":
metalPurity = 0.925;
break;
case "brown":
metalPurity = 0.80;
break;
case "green":
metalPurity = 0.50;
break;
default:
metalPurity = 0;
}
}else if(this.metal == "gold"){
switch(colorTest){
case "green":
metalPurity = "base metal or gold plated";
break;
case "milk colored":
metalPurity = "gold plated sterling silver";
break;
case "no color":
metalPurity = "real gold";
break;
default:
metalPurity = "Could be a fake, try different test";
}
}
return metalPurity;
}
this.metalValue = function(metal){
var sum = 0;
var spotPrice;
if (this.metal == "gold"){
spotPrice = 1000;
}else if(this.metal == "silver"){
spotPrice = 15;
}
sum = spotPrice * this.weight;
return sum;
}
}
var customerCindy = new PreciousMetals("silver", 2);
document.getElementById('testDiv').innerHTML = customerCindy.authentic("brown");
document.getElementById('testDiv2').innerHTML = customerCindy.metalValue(); //The result I would like would be 24 of course.
现在我意识到我可以这样做:
document.getElementById('testDiv2').innerHTML = customerCindy.metalValue() * customerCindy.authentic("brown");
然而,这里的目标是从真实方法中获取信息并使用它来帮助我计算metalValue方法中的金属值。
答案 0 :(得分:1)
如果您想在构造函数中将这两个方法的逻辑分开,则可以包含第三个执行将两个结果相乘的任务的方法。
var PreciousMetals = function(metal, weight){
this.metal = metal;
this.weight = weight; //weight in ounces
this.authentic = function(colorTest){
var metalPurity;
var zero = "";
if (this.metal == "silver"){
switch(colorTest){
case "brightred":
metalPurity = 1;
break;
case "darkred":
metalPurity = 0.925;
break;
case "brown":
metalPurity = 0.80;
break;
case "green":
metalPurity = 0.50;
break;
default:
metalPurity = 0;
}
}else if(this.metal == "gold"){
switch(colorTest){
case "green":
metalPurity = "base metal or gold plated";
break;
case "milk colored":
metalPurity = "gold plated sterling silver";
break;
case "no color":
metalPurity = "real gold";
break;
default:
metalPurity = "Could be a fake, try different test";
}
}
return metalPurity;
}
this.metalValue = function(){
var sum = 0;
var spotPrice;
if (this.metal == "gold"){
spotPrice = 1000;
}else if(this.metal == "silver"){
spotPrice = 15;
}
sum = spotPrice * this.weight;
return sum;
}
this.netValue = function(colorTest){
return this.authentic(colorTest) * this.metalValue();
}
}
这是一个有效的JSFiddle - https://jsfiddle.net/bwj3fv12/
答案 1 :(得分:1)
如果您想更新metalValue
以包含纯度检查的结果,您可以更新
this.metalValue = function(colorTest){
// ...
sum = spotPrice * this.weight * this.authentic(colorTest);
return sum;
}
并用
调用它customerCindy.metalValue('brown');
由于这只是一个例子,因此没有理由担心它,但可能现实世界中的纯度与金属和重量一样多,并且不会出现这种情况。 t实际上是这种方法的瞬态值。但那既不在这里,也不在那里。