我想獲取客戶的名字并將其用于其他功能。
我有一個地址:
John Doe 街道名稱 1 1234AA 村
function firstName(){
let address = "John Doe\nStreetname 1\n 1234AA Village";
let firstname = adres.split(' ')[0];
}
function customerText(){
firstName();
let text = "Dear " firstname ", how is life?";
alert(text);
}
所以結果會是:親愛的約翰,生活怎么樣?
但不幸的是,事實并非如此。不知道這應該如何作業?
uj5u.com熱心網友回復:
回傳名字并在第二個函式中使用它
function firstName(){
let address = "John Doe\nStreetname 1\n 1234AA Village";
let firstname = address.split(' ')[0];
return firstname;
}
function customerText(){
let text = "Dear " firstName() ", how is life?";
alert(text);
}
// call your function
customerText();
或將名字作為引數傳遞給第二個函式
function firstName(){
let address = "John Doe\nStreetname 1\n 1234AA Village";
let firstname = address.split(' ')[0];
customerText(firstname);
}
function customerText(nameParameter){
let text = "Dear " nameParameter ", how is life?";
alert(text);
}
// call your function
firstName();
uj5u.com熱心網友回復:
這是修改后的代碼:希望對您有所幫助。
function firstName() {
let address = "John Doe\nStreetname 1\n 1234AA Village";
let firstname = address.split(' ')[0];
customerText(firstname)
}
function customerText(fname) {
let text = "Dear " fname ", how is life?";
alert(text);
}
firstName()
uj5u.com熱心網友回復:
您可以獲取fName()incustomerText()函式的回傳值并將其直接放入文本中。
function fName(){
return 'Robert'
}
function customerText(){
return `Hi, ${fName()}, how are you :) ?`
}
console.log(customerText())
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/504881.html
標籤:javascript 功能
上一篇:使用條件渲染顯示/隱藏一組元素
下一篇:在ReactJS中更新陣列
