Cristobal Ruiz
Cristobal Ruiz

Reputation: 67

How to export variables from functions in Javascript?

I'm wondering how can I export a variable from inside a function to be used in another functions, I've used this code:

But in the latest function I'd like to add a variable called nombre from another function, but I'm not able, I thought about adding several values to a function, but at last, I only can imagine a function with endless parameters, if that's possible.

Thanks in advance! Cristobal.

<script>
//Empieza el script justo aquí
//Aquí definimos la función de cómo queremos llamar a nuestro personaje
//La función nombreintroducido recoge el valor de la variable nombre y la usa más adelante
  var nombrepersonaje = function() {
    var nombre = prompt("Como quieres que se llame tu personaje");
	nombreintroducido(nombre);
  }

//Aquí definimos que si el nombre tiene menos de tres carácteres, se repite la función nombrepersonaje
//Si se pulsa cancelar, se terminará el juego
//Si introduces algún nombre de personaje que sea válido, se abrirá un mensaje emergente que mostrará el nombre introducido
var nombreintroducido = function (nombre){
if (nombre === '') {
  confirm('Tu nombre de personaje ha de tener mas de 3 caracteres');
  nombrepersonaje();
} else if (nombre === null){
confirm('No has introducido ningun nombre de personaje, el juego terminara ahora')
}
else{
  confirm('Tu nombre de personaje es' + ' ' + nombre)

  }
};

var eligeclase = function(){
var clase = prompt("Que clase quieres elegir: Guerrero o Mago")
claseescogida(clase);
}

var claseescogida = function (clase){
if (clase === 'Guerrero'){
confirm('Has elegido la clase Guerrero: 10 Fuerza y 5 Inteligencia');
confirmaclase(clase);
}
else if (clase === 'Mago') {
confirm ('Has escogido la clase mago: 10 Inteligencia y 5 Fuerza');
confirmaclase(clase);
}
else {
confirm ('Tienes que escribir exactamente Guerrero o Mago');
eligeclase();
}};

var confirmaclase = function(clase) {
confirm('Tu clase es finalmente ' + clase + ' ... y tu nombre es');
}


//Se podría decir que el minijuego empezaría aquí, ya que lo anterior son funciones que definimos
nombrepersonaje();
eligeclase();



//Termina el script justo aquí
 </script>

Upvotes: 1

Views: 18952

Answers (2)

Dharmik Chauhan
Dharmik Chauhan

Reputation: 121

You can do it two ways:

  1. Declare var globally so that variable can be used from anywhere(inside or outside of java script) like.. var nombre; declared at top of javascript, and can be initialize and used within a javascript and from other java script as well. You need to just import the source js.
  2. Create a function which return the var itself. like.

    var myFunction = function(){return nombre;}

    When you need the nombre variable, just call the function as, var newNombre = myFunction();

Upvotes: 3

Niklas Vest
Niklas Vest

Reputation: 902

You can define a variable globally like this (For example in variables.js):

var myNombre;

Now whenever you want to prompt the user, you can assign the input to myNombre:

function initMyNombre () {
    // assign from input
    myNombre = prompt("Enter a nombre");
}

You variable now holds the input and keeps it until either you reassign it or close the browser tab.

Then, maybe in your index.html, you can use myNombre:

function anyFunction () {
    // x now has the value of myNombre
    var x = myNombre;
}

Note that you need to include variables.js in your html document with <script src="directory/variables.js"></script>. Also have a look at This W3schools Tutorial. Eventhough W3schools isn't the best practice for seasoned programmers, it can give you a good introduction.

Upvotes: 0

Related Questions