admin管理员组文章数量:1399474
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>
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>
Share
Improve this question
asked Dec 24, 2015 at 19:32
Cristobal RuizCristobal Ruiz
672 gold badges2 silver badges9 bronze badges
2
- Just use global variables so that they can be referenced and altered anywhere – Nick Zuber Commented Dec 24, 2015 at 19:37
- And how can I do that? How can I use global variables? I'm just doing a course in another site, I started learning few days ago, don't know why I was voted -1, there's no place for newbies here? :( The variable is defined by a prompt in a function, so if it's written in a function and asked in a function, it can't be global, right? How then? – Cristobal Ruiz Commented Dec 24, 2015 at 19:40
2 Answers
Reset to default 3You can do it two ways:
- 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. 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();
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.
本文标签: How to export variables from functions in JavascriptStack Overflow
版权声明:本文标题:How to export variables from functions in Javascript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744137275a2592455.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论