JavaScript:闭包帮助

发布于 2024-08-20 02:57:19 字数 590 浏览 4 评论 0原文

我有一个空的 .js 文件,其中包含以下代码:

Cart.CheckoutNow = {
 ...
}

// Alias

if (typeof(CheckoutNow) === 'undefined') {
    CheckoutNow = Cart.CheckoutNow;
}
else {
    alert('CheckoutNow is already a variable in window.');
}

如何让 // Alias 代码出现在页面顶部,但在声明 Cart.CheckoutNow 之后执行它?

这可行,但我不喜欢你必须在底部调用alert():

alias = function() {
 if (typeof(CheckoutNow) === 'undefined') {
  CheckoutNow = Cart.CheckoutNow;
 }
 else {
  alert('CheckoutNow is already a variable in window.');
 }
};

Cart.CheckoutNow = {
 ...
};

alias();

I have an empty .js file with this code in it:

Cart.CheckoutNow = {
 ...
}

// Alias

if (typeof(CheckoutNow) === 'undefined') {
    CheckoutNow = Cart.CheckoutNow;
}
else {
    alert('CheckoutNow is already a variable in window.');
}

How can I have the // Alias code appear at the top of the page, but have it execute after Cart.CheckoutNow is declared?

This would work, but I don't like that you have to call alert() at the bottom:

alias = function() {
 if (typeof(CheckoutNow) === 'undefined') {
  CheckoutNow = Cart.CheckoutNow;
 }
 else {
  alert('CheckoutNow is already a variable in window.');
 }
};

Cart.CheckoutNow = {
 ...
};

alias();

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

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

发布评论

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

评论(3

心在旅行 2024-08-27 02:57:19

如果没有解释为什么要尝试这样做,我会猜测这是为了更好的代码组织。如果是这种情况,我会将您的 JavaScript 分成多个文件,并按以下顺序包含在您的页面上:

// namespace.js
if (typeof Cart === 'undefined') {
    Cart = {};
}

// checkoutnow.js
Cart.CheckoutNow = {
    // module code here
}

// alias.js
alias = function() {
    // alias code here
}

// domready.js
onDocumentReadyFunction() {
    alias();
}

Without an explanation of why you're trying to do this, I'm going to guess that it's for better code organization. If that's the case, I would split your JavaScript up into multiple files something along the lines of this, and to be included on your page in this order:

// namespace.js
if (typeof Cart === 'undefined') {
    Cart = {};
}

// checkoutnow.js
Cart.CheckoutNow = {
    // module code here
}

// alias.js
alias = function() {
    // alias code here
}

// domready.js
onDocumentReadyFunction() {
    alias();
}
猥︴琐丶欲为 2024-08-27 02:57:19

我不确定这是否真的是您想要做的,但如果您确定,那么您想要的是:

window.setTimeout(function(){

  // Alias
  if (typeof CheckoutNow === 'undefined') {
    CheckoutNow = Cart.CheckoutNow;
  }
  else {
    alert('CheckoutNow is already a variable in window.');
  }

},0);

Cart.CheckoutNow = {
  ...
}

I'm not sure this is really what you want to be doing, but if you're sure, then what you want is:

window.setTimeout(function(){

  // Alias
  if (typeof CheckoutNow === 'undefined') {
    CheckoutNow = Cart.CheckoutNow;
  }
  else {
    alert('CheckoutNow is already a variable in window.');
  }

},0);

Cart.CheckoutNow = {
  ...
}
丿*梦醉红颜 2024-08-27 02:57:19

您必须在函数中定义 Alias 并将其称为 body 的 onload 事件。

You would have to define Alias in a function and call it onload event of body.

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