简体   繁体   English

在数组中存储函数

[英]Storing functions within arrays

I'm having a issue with something very simple. 我有一个非常简单的问题。 I am just wondering as to I can store these functions within an array. 我只是想知道我是否可以将这些函数存储在数组中。 Check out some of the code below. 查看下面的一些代码。 I am unsure as to if this is correct as to how I am storing these functions. 我不确定这对我如何存储这些功能是否正确。 I am unsure as to if these functions should be within a object literal or array.This is not necessarily for a project, just good practice. 我不确定这些函数是否应该在对象文字或数组中,这不一定是项目的一种好习惯。 Thanks! 谢谢!

//declaring a function 
function alert_name(){
    //declaring variables within a function asking user their name.
    var username =  prompt("Hey there, what is your name."," ");
//generating user input
    var chameleon = "Welcome " + username;
//combinators 
    //alert("Welcome " + chameleon+ ", This is 'the website");
};
// inserting quotes into a string that is being alerted from the browser.
function otherTHings(){
    var single = 'He said \'RUN\' ever so softly.';
    //alert(single);
};
//running these functions and actually carry out the operations 
//that have actually been declared into code above. 

//string operations
function string_opertaions(){
    var complete = "Com" + "plete";
    //alert(complete);
    // using combinators to do the same thing. 
    var sentance1 = "My name is";
    var sentance2 = "someone";
    var  totalsenatces = sentance1 += sentance2;
    //alert(totalsenatces);
};

//Booleans or true false values
function booleanys(){
    var lying = false;
    var truthful = true;
};
//Arrays very important. very similar to a object literal but different.
//Arrays store information or values or varibales/data. 
var rack = [];
rack[0] = alert_name();
rack[1] = otherTHings();
rack[2] = string_opertaions();
rack[3] = booleanys();
//alert_name();
//otherTHings();
//string_opertaions();
//booleanys();

You are invoking the function and storing the result! 您正在调用该函数并存储结果!

var rack = [];
rack[0] = alert_name;
rack[1] = otherTHings;
rack[2] = string_opertaions;
rack[3] = booleanys;

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

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