这个javascript的目的,它使用了什么样的设计模式?

发布于 2024-10-01 01:35:10 字数 308 浏览 2 评论 0原文

    if (!window['console']) {
        window.console = {
        log: function(msg) {}
        }
    }


$(window).ready(function() {
    Site.onReady();
});



    var Site = {
        host: null,
        path: null,
        etc..

还有var Helpers,var Site,看起来不错,但是看不懂用途?有谁知道吗?

    if (!window['console']) {
        window.console = {
        log: function(msg) {}
        }
    }


$(window).ready(function() {
    Site.onReady();
});



    var Site = {
        host: null,
        path: null,
        etc..

And there have var Helpers, var Site, looks pretty good, but can't understand the purpose? Anyone who knows that?

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

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

发布评论

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

评论(3

撧情箌佬 2024-10-08 01:35:10
if (!window['console']) {
    window.console = {
    log: function(msg) {}
    }
}

这会检查当前是否已分配给 window.console 任何内容,如果没有,它会分配一个具有“日志”功能的自定义对象。这使得 window.console.log 无论如何都可用,并且如果已经有该函数的本机(或早期定义的)版本,则将使用它。

$(window).ready(function() {
    Site.onReady();
});



    var Site = {
        host: null,
        path: null,
        etc..

我不知道这是做什么用的,但是 Site 在被放入 $(window).ready() 的匿名回调中时是未定义的,这是一个东西应该避免这种情况(只需将 $(window).ready() 放在定义站点的下面)

至于这个特定的代码片段:

$(window).ready(function() {
        Site.onReady();
    });

这将一个匿名函数传递给 $(window).ready() 函数,该函数将在以下情况下调用它: DOM 已准备就绪。直接使用匿名函数可以避免命名函数然后再将其传入的需要。

function myFunc() { //我们现在可以在任何地方使用 myFunc,这可能是不需要的
Site.onReady();
最后

$(window).ready(myFunc);

   var Site = {
            host: null,
            path: null,
            etc..

var myVar = {key1:"value", key2:"other_value"}; 语法创建一个带有键和值的新对象,可以像这样使用:myVar.key1 =“新值!”

if (!window['console']) {
    window.console = {
    log: function(msg) {}
    }
}

This checks to see if there's anything currently assigned to window.console already and if there's not, it assigns a custom object that has a 'log' function. This makes window.console.log usable no matter what, and if there's already a native (or earlier defined) version of the function, it will be used.

$(window).ready(function() {
    Site.onReady();
});



    var Site = {
        host: null,
        path: null,
        etc..

I have no idea what this is for, but Site is undefined at the time it is placed into the anonymous callback for $(window).ready(), which is something that should be avoided (just place the $(window).ready() below where site is defined)

As for this specific snippet:

$(window).ready(function() {
        Site.onReady();
    });

this passes an anonymous function to the $(window).ready() function, which will call it when the DOM is ready. Using an anonymous function directly avoids the need to name a function and then pass it in later.

function myFunc() { //we can use myFunc anywhere now, which may be unwanted
Site.onReady();
}

$(window).ready(myFunc);

Lastly:

   var Site = {
            host: null,
            path: null,
            etc..

The var myVar = {key1:"value", key2:"other_value"}; syntax creates a new object with keys and values that can be used like this: myVar.key1 = "newValue!"

薆情海 2024-10-08 01:35:10

看起来它初始化了页面上预期的几个全局对象。例如 console,它在 Firefox/Firebug 中可用于日志记录,但在其他浏览器中则不可使用。因此,通过检查 window['console'] 是否存在并在必要时添加它,您可以信任可以调用 console.log() 的 JavaScript 代码,而不会导致错误。

我认为 Site、Helpers 等都会做类似的事情。

Looks like it initializes several global objects that are expected on the page. For example console, which is available in Firefox/Firebug for logging, but not other browsers. So by checking for existence of window['console'] and adding it when necessary, you can trust in the JavaScript code you can call console.log() without causing an error.

I assume Site, Helpers, etc all do something similar.

黑白记忆 2024-10-08 01:35:10

它在窗口对象上定义一个“控制台”对象文字(如果它尚不存在),它有一个函数日志。 您也可以在代码中编写代码。

console.log('something')

这意味着即使浏览器不支持,

its defining a 'console' object literal on the window object, if it is not already there, which has a function log. This means in your code you can write

console.log('something')

even if the browser doesn't support it.

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