JavaScript:单独声明和定义函数?

发布于 2024-09-14 07:48:04 字数 472 浏览 2 评论 0原文

如果我想给一个 JavaScript 变量全局作用域,我可以轻松地做到这一点:

var myVar;

function functionA() {
    myVar = something;
}

是否有一种类似的简单而干净的方法(无需创建对象)来分离嵌套函数的“声明”和“定义”?例如:

function functionB;       // declared with global scope

function functionA() {

    functionB() {        // nested, would have local scope if declared here
        CODE;
    }
}

我应该澄清,我指的是函数名称本身的范围 - 这样,如果它位于 iframe 中,则可以从父文档中的脚本调用它。 (与嵌套函数内的变量范围无关。)

If I want to give a JavaScript variable global scope I can easily do this:

var myVar;

function functionA() {
    myVar = something;
}

Is there a similarly simple and clean way -- without creating an object -- to separate the "declaring" and the "defining" of a nested function? Something like:

function functionB;       // declared with global scope

function functionA() {

    functionB() {        // nested, would have local scope if declared here
        CODE;
    }
}

I should clarify that I'm referring to the scope of the function name itself -- so that if it is in an iframe it can be called from a script in the parent document. (Nothing to do with scope of variables inside nested functions.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

内心旳酸楚 2024-09-21 07:48:04

您可以通过在 window 对象上创建实例来创建全局变量和函数:

function _A()
{
    // scoped function
    function localFunctionInTheScopeOf_A()
    {
    }

    // global function
    window.globalFunctionOutsideTheScopeOf_A = function ()
    {
    };
}

不过,在您的情况下,您需要做的就是这样:

var myFn; // global scope function declaration

function randomFn()
{
    myFn = function () // global scope function definition
    {
    };
}

注意:阻塞永远不是一个好主意全球范围。如果可以的话;我建议您重新思考代码的工作原理,并尝试封装您的代码。

You can create global variables and functions by creating instances on the window object:

function _A()
{
    // scoped function
    function localFunctionInTheScopeOf_A()
    {
    }

    // global function
    window.globalFunctionOutsideTheScopeOf_A = function ()
    {
    };
}

In your case, though, all you need to do is this:

var myFn; // global scope function declaration

function randomFn()
{
    myFn = function () // global scope function definition
    {
    };
}

Note: It is never a good idea to clog up the global scope. If you can; I'd recommend that you re-think how your code works, and try to encapsulate your code.

红衣飘飘貌似仙 2024-09-21 07:48:04

也许我误解了这个问题,但听起来你想要这样的东西:

var innerFunc;

function outerFunc() {
    var foo = "bar";

    innerFunc = function() {
        alert(foo);
    };
}

Perhaps I'm misunderstanding the question, but it sounds like you want something like this:

var innerFunc;

function outerFunc() {
    var foo = "bar";

    innerFunc = function() {
        alert(foo);
    };
}
没有伤那来痛 2024-09-21 07:48:04

您无法以这种方式跨窗口/iframe 全局化变量/函数。每个窗口/iframe 都有自己的全局范围,并且要在另一个窗口/iframe 中定位变量/函数,您需要显式访问器代码并符合 同源政策。仅可访问 windows/iframe 全局范围内的变量/函数。

顶部窗口中的代码。

var iframe = document.getElementById('iframeId');
var iframeContext = iframe.contentWindow || iframe;

// this will only work if your iframe has completed loading
iframeContext.yourFunction();

您也可以在顶部窗口中定义函数/变量,并通过闭包从 iframe 绑定您需要的内容,从而在一个范围内简单地工作。再次假设您符合同源政策。这不能跨域工作。

iframe 中的代码。

var doc = document;
var context = this;
top.myFunction = function(){
    // do stuff with doc and context.
}

还需要注意的是,您需要检查 iframe 内容及其脚本是否已完全加载。您的首页/窗口将在 iframe 内容完成之前无意中完成并运行,因此变量/函数可能尚未声明。

至于公开私有函数,其他人已经提出了这一点,但为了完整性而复制/粘贴。

var fnB;
var fnA = function(){
    var msg = "hello nurse!";
    fnB = function(){
         alert(msg);
    }
}

我习惯将独立函数声明为变量(函数表达式),并且仅使用函数语句来表示构造函数/伪类。它还避免了一些可能的尴尬错误。无论如何,fnB 驻留在 iframe 的全局范围内,并且可供顶部窗口使用。

为什么你想要这个让我困惑,几个月后调试或更新似乎会让事情变得更加复杂。

You cannot globalize variables/functions cross windows/iframes that way. Each window/iframe has it's own global scope and to target variables/functions in another window/iframe, you need explicit accessor code and conform to the same origin policy. Only variables/functions inside the windows/iframes global scope are accessible.

code in top window.

var iframe = document.getElementById('iframeId');
var iframeContext = iframe.contentWindow || iframe;

// this will only work if your iframe has completed loading
iframeContext.yourFunction();

You could also possibly define functions/variables in the top window instead and simply work in one scope by binding the stuff you need from the iframe through a closure. Again, assuming you meet the same origin policy. This will not work cross domain.

code in iframe.

var doc = document;
var context = this;
top.myFunction = function(){
    // do stuff with doc and context.
}

It is also important to note, that you need to check if your iframe content and it's scripts are fully loaded. Your top page/window will inadvertidly be done and running before your iframe content is done, ergo variables/functions might not be declared yet.

As for exposing a private function, others have awnsered this, but copy/pasting for completeness.

var fnB;
var fnA = function(){
    var msg = "hello nurse!";
    fnB = function(){
         alert(msg);
    }
}

I have the habbit of declaring stand alone functions as variables (function expression) and only use function statements to signify constructors/pseudo-classes. It also avoids a few possible embarrasing mistakes.. In any case, fnB resides in the global scope of the iframe and is available to the top window.

Why exactly you want this beats me, seems it makes matters more complicated to debug or update a few months later.

故人的歌 2024-09-21 07:48:04

你可以做你想做的事。

您可以创建一个类似于属性和方法的命名空间的函数,然后您实际上可以调用...

functionB();

functionA.functionB();

这里有一篇关于如何执行此操作的文章:

http://www.stevefenton.co.uk/Content/Blog/Date/201002/Blog/ JavaScript-Name-Spacing/

响应更新...

iframe 是否与父网站位于同一域中?你不能跨域边界调用 JavaScript,这或许可以解释这个问题。

You can kind of do what you want.

You can create a function that acts like a namespace for properties and methods, and then you could essentially call either...

functionB();

or

functionA.functionB();

There is an article on how to do it here:

http://www.stevefenton.co.uk/Content/Blog/Date/201002/Blog/JavaScript-Name-Spacing/

In response to the update...

Is the iframe on the same domain as the parent site? You can't call JavaScript across the domain boundary, which may explain the problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文