gpt4 book ai didi

javascript - 如何重写 js/varien/product.js 中的 JS 函数?

转载 作者:行者123 更新时间:2023-12-02 19:04:26 27 4
gpt4 key购买 nike

我正在制作一个 Magento 扩展,它在产品 View 页面上调用自定义 JS 文件。此自定义 JS 文件将最后加载,并且需要覆盖/js/varien/product.js 底部的 formatPrice() 函数。

原formatPrice函数如下:

formatPrice: function(price) {
return formatCurrency(price, this.priceFormat);
}

我想用以下内容替换/覆盖此函数:

formatPrice: function(price) {
if (price % 1 == 0) { this.priceFormat.requiredPrecision = 0; }

return formatCurrency(price, this.priceFormat);
}

如何在自定义 JS 文件中编写 JS 代码,以便它正确覆盖此函数?我对 JS 还不够了解。

最佳答案

如果它是全局的,那么您可以执行window.formatPrice = myNewFormatPrice;如果它是对象的成员,那么您可以执行以下操作:anObject.formatPrice = myNewFormatPrice;

如果您需要编辑对象的原型(prototype),请使用:Product.OptionsPrice.prototype.formatPrice = myFormatPrice;

您还需要研究对 requiredPrecision 的访问。如果它是“私有(private)”或“ protected ”,那么您将无法访问它。

关于javascript - 如何重写 js/varien/product.js 中的 JS 函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14464492/

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