简体   繁体   English

创建一个显示警报的通用函数,并接受要在警报中显示的值作为输入

[英]create a generic function that displays an alert, and accepts as input the value that is to be displayed in the alert

Function gets called. 函数被调用。 A JavaScript function is a block of code designed to perform a particular task. JavaScript函数是旨在执行特定任务的代码块。

A JavaScript function is executed when "something" invokes it (calls it). 当“某物”将其调用(调用)时,将执行JavaScript函数。

function displays_Five() {
  window.alert("Five");
}
function displays_Four() {
  window.alert("Four");
}
function displays_Three() {
  window.alert("Three");
}
function displays_Two() {
  window.alert("Two");
}
function displays_One() {
  window.alert("One");
}

Here's one way: 这是一种方法:

window.alert("Five");
window.alert("Four");
window.alert("Three");
window.alert("Two");
window.alert("One");

Now if you really want to create a wrapper function: 现在,如果您真的想创建包装函数:

function myAlert(str) {
    window.alert(str);
}
myAlert("Five");
myAlert("Four");
myAlert("Three");
myAlert("Two");
myAlert("One");

 function myAlert(str) { window.alert(str); } myAlert("Five"); myAlert("Four"); myAlert("Three"); myAlert("Two"); myAlert("One"); 

function showAlert(contents) {
    alert(contents);
}

But why not just use the alert function itself? 但是,为什么不只使用警报功能呢?

simple as: 简单为:

function displayAlert(text) {
    alert(text);
}

// Prompt from client
var input = prompt('What you want?');

// Make sure input is not empty (NULL) then display client input
if(input) displayAlert(input);

// Or display static text
displayAlert('Well Done.')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM