这段 Javascript 代码有什么作用? [英] What does this Javascript code do?

查看:21
本文介绍了这段 Javascript 代码有什么作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在查看 Sharepoint 脚本文件,但遇到了一些我不明白的问题:

I've been looking at Sharepoint script files and I've come across this bit that I don't get:

function ULSTYE() {
    var o = new Object;
    o.ULSTeamName = "Microsoft SharePoint Foundation";
    o.ULSFileName = "SP.UI.Dialog.debug.js";

    return o;
}

SP.UI.$create_DialogOptions = function() {
    ULSTYE:;   <----------------------------- WTF?
    return new SP.UI.DialogOptions();
}

实际上,这个文件中的每个函数定义都以相同的 ULSTYE:; 行开始,紧跟在左大括号之后.有人能解释一下第二个函数中的第一行是做什么的吗?

Actually every function definition in this file starts with the same ULSTYE:; line right after the opening brace. Can anybody explain what does the first line in the second function do?

例如,Firefox/Firebug 将这个函数解释为我也无法理解的东西:

Firefox/Firebug for instance interprets this function as something that I can't understand either:

function () {
    ULSTYE: {
    }
    return new (SP.UI.DialogOptions);
}

而且我以为我彻底了解 Javascript... ;) 一定是我过去从未使用过的一些晦涩的功能,显然其他人也很少使用.

And I thought I knew Javascript through and through... ;) Must be some obscure feature I never used in the past and is obviously seldomly used by others as well.

推荐答案

第一位定义了一个函数,该函数创建一个具有几个属性的对象并返回它.我想我们都清楚这一点.:-)

The first bit defines a function that creates an object with a couple of properties and returns it. I think we're all clear on that bit. :-)

不过,第二点是不使用那个函数.它定义了一个同名的 label.虽然它使用相同的字符序列,但它不是对上述函数的引用.Firefox 的解释和其他任何东西一样有意义,因为标签后面应该有它可以引用的东西.

The second bit, though, is not using that function. It's defining a label with the same name. Although it uses the same sequence of characters, it is not a reference to the function above. Firefox's interpretation makes as much sense as anything else, because a label should be followed by something to which it can refer.

有关标记语句的更多信息,请参阅规范.

For more about labelled statements, see Section 12.12 of the spec.

离题:我会避免使用此来源的代码.编写它的人显然对 JavaScript 相当陌生,并没有表现出多少迹象表明他们知道自己在做什么.例如,他们将 () 放在 new Object() 调用之外,虽然这是允许的,但这是相当狡猾的事情.他们可能会争辩说他们这样做是为了节省空间,但如果是这样,他们最好使用对象字面量:

Off-topic: I would avoid using code from this source. Whoever wrote it is apparently fairly new to JavaScript and doesn't show much sign that they know what they're doing. For instance, they've left the () off the new Object() call, and while that's allowed, it's fairly dodgy thing to do. They could argue that they were doing it to save space, but if they were, they'd be better off using an object literal:

function ULSTYE() {
    return {
        ULSTeamName: "Microsoft SharePoint Foundation",
        ULSFileName: "SP.UI.Dialog.debug.js"
    };
}

根本没有太多理由编写new Object(){} 在功能上是相同的.

There's never much reason to write new Object() at all; {} is functionally identical.

当然,第二位根本没有任何理由.:-)

And, of course, there's no justification for the second bit at all. :-)

这篇关于这段 Javascript 代码有什么作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆