Javascript 在闭包内返回函数返回未定义的

Javascript 在闭包内返回函数返回未定义的,javascript,function,module,return,closures,Javascript,Function,Module,Return,Closures,我在JavaScript中有一个封闭的函数,如下所示: var myFunction = function (options) { function blah() { var blahString = options.blahString; //more blah } function blah2() { //blah2 } return { blah : function { retur

我在JavaScript中有一个封闭的函数,如下所示:

var myFunction = function (options) {
    function blah() {
        var blahString = options.blahString;
        //more blah
    }

    function blah2() {
        //blah2
    }

    return {
        blah : function { return blah(); },
        blah2 : function { return blah2(); }
    }
};
当我在HTML中时,我试图调用
myFunction.blah()
,它告诉我
对象没有方法“blah”

如何访问全局范围中返回的函数

谢谢

var myFunction = (function (options) {
  function blah() {
    return options.a;
  }

  function blah2() {
    //blah2
  }

  return {
    blah: function() { return blah(); },
    blah2: function() { return blah2(); }
  };
});

alert(myFunction({a:1, b:2}).blah());

这个很好用。注意
blah:function
这只是解释了为什么它不工作以及如何使它工作。对于学习来说,这就足够了。事实上,你应该解释一下你想要达到的目标,这样其他人就能引导你朝着正确的方向前进

// A scope of a function is activated ONLY when it is invoked

// Let us define a function
var myFunction = function (options) {
    function blah() {
        alert("I am blah");
    }

    function blah2() {
        //blah2
    }
    alert("I am active now and I am returning an object");
    return {
        blah: function () {
            return blah();
        },
        blah2: function () {
            return blah2();
        }
    };
};

myFunction.blah3 = function () {
    alert("I am blah3");
};

// myFunction is not invoked, but justed used as an identifier. 
// It doesn't have a method blah and gives error
myFunction.blah();

// blah3 is a static method of myFunction and can be accessed direclty using myFunction  
myFunction.blah3();

// myFunction is invoked, which returns an object
// it contains the function blah
myFunction().blah();

// or
var myObject = myFunction();
myObject.blah();
myObject.blah2();

您或者想要拥有
var myFunction=(函数(选项){})()myFunction().blah()
那样调用它。我想你是想得到我的第一个建议,如果它有参数,可以调用
(函数(选项){})()?我问,因为
myFunction(options)
应该在HTML中调用,因为这是一个插件。我甚至没有考虑过<代码>选项>代码>。所以你可能想要第二种方式。您必须调用它,比如
var something=myFunction({a:1,b:2});诸如此类有效!多谢各位
myFunction(options)
不应该立即调用,应该在HTML中调用,并将JSON对象作为
options
传递。如果我在声明的末尾包含(),它将在没有任何选项的情况下被调用,并且会出错。我将编辑该示例,使其需要options变量。将myFunction保存为对象可以使其正常工作-这似乎是我理解JavaScript对象时遇到的问题。非常感谢。