What does this Javascript code do?

前端 未结 4 1874
孤独总比滥情好
孤独总比滥情好 2020-12-30 03:28

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         


        
4条回答
  •  粉色の甜心
    2020-12-30 04:07

    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. :-)

    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.


    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"
        };
    }
    

    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. :-)

提交回复
热议问题