función dentro de la función javascript

I have a .js file which looks something like this:

function regionmap () { 
var width = 960,
    height = 500,

var projection = d3.geo.albersUsa()
    .translate([width / 2, height / 2]);

var path = d3.geo.path()

var svg ="body").append("svg")
    .attr("width", width)
    .attr("height", height)

var states = svg.append("g")
    .attr("id", "states")

    d3.json("readme.json", function(json) {"svg").selectAll("path")
          .attr("d", path)
          .on("click", clicked)
          .text(function(d) { return; });

listofnames = new Array();

function clicked (d) { 

    var regionname =;

    var currentclass ="id") ;

    if (currentclass == "active") {"id", "nonactive");
    } else {"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;

    if (contains == false){
    } else if(contains == true){


    var x=document.getElementById("demo");

    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?

preguntado el 26 de junio de 13 a las 14:06

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. -

Then just declare it as window.b || (window.b = function () {}); -

This is just the way how the javascript scope works -

4 Respuestas

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();

Now internal b function is globally accessible, while a is still private.

Respondido el 26 de junio de 13 a las 14:06

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(){
        a: function(){
           // do something on a
        b: function(){
           // do something on b

// Call like so:

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

Intenta agregar window.b = b después de function b().

Respondido el 26 de junio de 13 a las 14:06

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();

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 or haz tu propia pregunta.