función dentro de la función javascript
Frecuentes
Visto 230 equipos
0
I have a .js file which looks something like this:
function regionmap () {
var width = 960,
height = 500,
centered;
var projection = d3.geo.albersUsa()
.scale(width)
.translate([width / 2, height / 2]);
var path = d3.geo.path()
.projection(projection);
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height)
.attr("id","svg");
var states = svg.append("g")
.attr("id", "states")
d3.json("readme.json", function(json) {
d3.select("svg").selectAll("path")
.data(json.features)
.enter()
.append("path")
.attr("d", path)
.on("click", clicked)
.append("title")
.text(function(d) { return d.properties.name; });
});
listofnames = new Array();
listofnames.push("Regions:");
function clicked (d) {
var regionname = d.properties.name;
var currentclass = d3.select(this).attr("id") ;
if (currentclass == "active") {
d3.select(this).attr("id", "nonactive");
} else {
d3.select(this).attr("id", "active");
}
var contains;
var index;
for (var i = 0; i < listofnames.length; i++) {
if (regionname != listofnames[i]) {
contains = false;
} else {
contains = true;
index = i;
break;
}
}
if (contains == false){
listofnames.push(regionname);
} else if(contains == true){
listofnames.splice(index,1);
}
var x=document.getElementById("demo");
x.innerHTML=listofnames;
}
function sendingvariable (){
window.location.href = "../php/fileregions.php?name=" + listofnames;
}
}
The thing is that when calling the function from html I first call the function regionmap on click ( onclick="regionmap()) which works good. However, I then need to call the function sendingvariable from the html, and I am not able. Any way to solve this?
4 Respuestas
1
You can make it visible for the outer scope using a variation of the modular pattern:
function global() {
function a() {};
function b() {};
// public api
return {
b: b
}
}
var glob = global();
glob.b();
Now internal b
function is globally accessible, while a
is still private.
Respondido el 26 de junio de 13 a las 14:06
0
The functions a and b are hidden from anything outside the global function scope (called a cierre). You can return something from the global function (an object) that contains the a and b functions. You can then call these functions from there:
function global(){
return{
a: function(){
// do something on a
},
b: function(){
// do something on b
}
}
}
// Call like so:
global().a();
Respondido el 26 de junio de 13 a las 14:06
It works but I still have a problem. I just did the return for function b, in function b I use a variable declared in global but updated in a. The thing is that when using b I need that variable which has been updated from a. Like I did it, it doesn't get the updated variable. Any idea to solve this? - Nuri
If that is what you want you need to create an instance of the global function (which will become an object on itself). var myGlobal = new global(); myGlobal.a(); - Bas escoria
0
Intenta agregar window.b = b
después de function b()
.
Respondido el 26 de junio de 13 a las 14:06
0
If you need to call a() without anything from global() local variables you can just declare it as separate function. If you need OOP model you should create object from global and then call a(). Also declaration have to look:
function global() {
var a = function() { ... }
}
Y luego puedes llamarlo así:
var obj = new global();
obj.a();
Sorry if I didn't understand your question right.
Respondido el 26 de junio de 13 a las 14:06
No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas javascript function or haz tu propia pregunta.
Show us a little more code please? which variables? (because right now my solution is, remove the
}
from line 4 and move it to the end of line 1. Then function a and b both work. - BiketireStudy up on the Module pattern here: adecuadamentegood.com/JavaScript-Module-Pattern-In-Depth.html - n8wrl
Then just declare it as
window.b || (window.b = function () {});
- IanThis is just the way how the javascript scope works - jantimon