gpt4 book ai didi

javascript - 如何从自定义方法获取信息并将其应用到我的构造函数 javascript 中的另一个方法?

转载 作者:行者123 更新时间:2023-12-02 16:15:59 24 4
gpt4 key购买 nike

好吧,我试图想一个很好的例子,所以我创建了一个关于贵金属的构造函数。构造函数采用金属类型和重量。我有两个方法。一种方法确定贵金属(黄金或白银)是否真实,另一种方法根据现货价格计算值(value)。 (我知道现货价格是错误的,无论如何这只是一个例子)。

假设一位顾客带来了一 block 银含量为 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 方法中的金属值。

最佳答案

如果您希望在构造函数中将这两个方法的逻辑分开,则可以包含第三个方法来执行将两个结果相乘的任务。

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/

关于javascript - 如何从自定义方法获取信息并将其应用到我的构造函数 javascript 中的另一个方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29596315/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com