[英]How to return value from function parameter using javaScript?
I ran into such a problem, in my App, I have a variable that contains productId, Also I have a function which returns productId, I wonder if that is possible to pass productId
function into addToCard()
function as a parameter and return value,我試圖得到類似的結果addToCard('223343445')
,有可能達到這個結果嗎? 謝謝
let singleProductId = '223343445' productId(singleProductId) function productId(parameter){ return parameter } function addToCard(param){ console.log(`${param} added to the card`) }
<button onclick=addToCard(productId())>add to cart</button>
只需將 function 傳遞到addToCard
並在其中調用它。
let singleProductId = "223343445"; function productId(parameter) { return parameter; } function addToCard(productId) { console.log(`${productId(singleProductId)} added to the card`); } const button = document.querySelector("button"); button.addEventListener("click", e => { addToCard(productId); })
<button>add to cart</button>
請遵循這種方法
const singleProductId = '223343445';
const productId=(parameter)=>{
return parameter
}
const addToCard=(param)=>{
param=productId(singleProductId)
console.log(`${param} added to the card`)
}
<button onclick=addToCard(productId())>add to cart</button>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.