gpt4 book ai didi

javascript - 当前项目告诉我需要查看 Javascript OOP,我应该吗?

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:28:16 25 4
gpt4 key购买 nike

<分区>

我正在从事(并且实际上已经完成)一个项目,该项目根据用户输入的数据获取报价。我已经编写了代码并且运行良好。它是使用事件和函数用 javascript/jQuery 编写的,更像这样(我只是为这个问题写的例子,不是实际代码):

// event - when the quantity field changes
$('input[name=quantity]').change(function(){
// get the product
var product = $('input[name=product]').val();
// run function to set the base price
setBasePrice(this.value,product);
});

// function - set the base price
function setBasePrice(quantity,product){
var basePrice = quantity * getTierPrice(quantity,product);
// show the base price to user
$('.basePrice').empty().val(basePrice);
};

// function - set price based on quantity and product
function getTierPrice(quantity,product){
// switches through 6 tiers (based on quantity)
// e.g. - If product is x and quantity is y, price = z
// returns a value
};

还有更多的工作部分,但我写这些是为了说明它是如何组合在一起的。它看起来真的很冗长并且有很多移动部分。到了更新它的时候,这很痛苦。

但我想知道我是否应该为这样的事情使用 OOP?我真的只是想找到 javascript 最佳实践的根源,并找出应该如何真正将这样的东西放在一起。我觉得我写的很多代码虽然有效,但并不总是最好的方法,也许是因为我是自学的,还没有真正深入挖掘。

如果有助于理解正在发生的事情,我会将功能分解成多个部分。了解每一位的工作原理对您来说并不重要:

Things I'm expecting 

= zip code (number)
= product (string)
= quantity (number)
- Need to verify number is between x and y (e.g. - 100 to 10000)
= artwork colors (number)
- Need to verify number is between x and y (e.g. - 0 to 6)

Things I'm calculating

= base price (number)
- Depends on the product selected
- Depends on the quantity entered (6 tiers of pricing based on quantity)
e.g. - "Product 1" quantity of 101 to 200 = $9, quantity of 201 to 300 = $7, etc

= screen charge (number)
- Depends on the number of artwork colors entered
- Depends on the quantity entered (uses same tier as when calculating base price)
e.g. - "Product 1" quantity of 101 to 200 add 1.00 per artwork color
- Gets rolled into the base price
e.g. - base price = base price + (artwork colors*quantity);

= shipping cost (hits url via ajax to fetch rate, returns number)
- Depends on zip code entered
- Depends on product selected (each product has different weight)

Things I need to output

= final price
= shipping cost
= base price

那么我应该使用 OOP,我应该看看 YUI Module Pattern 吗? ?如果是这样,在高层次上,我将如何开始?我不需要完整的代码示例,只需要知道从哪里开始。我真的希望我的代码看起来不错并且结构正确。非常感谢任何建议、资源链接等。

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