gpt4 book ai didi

javascript - 这段 JavaScript 代码有什么作用?

转载 作者:行者123 更新时间:2023-12-02 22:52:35 25 4
gpt4 key购买 nike

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

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:; 行开头。谁能解释一下第二个函数的第一行是做什么的?

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

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

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

最佳答案

在思考这个问题很长一段时间后,我终于坐下来解决了。它是用于收集客户端诊断信息的相对复杂机制的一部分,其中包括将 javascript 调用堆栈(包括函数名称和 javascript 文件)发送回服务器的能力。

查看文件 init.debug.js 的前 250 行,该文件位于

%Program Files%\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\LAYOUTS\1033\init.debug.js

该文件定义了客户端上“ULS”实现的所有函数。

当然,您需要安装 SharePoint 2010 才能使该文件存在于本地计算机上。

更新--以下概述了该机制的工作原理。真正的实现不仅仅如此

考虑下面的 html 页面,其中包含一些 js,每个 js 都可以相互调用。

<html>
<head>
<script type="text/javascript" src="ErrorHandling.js"></script>
<script type="text/javascript" src="File1.js"></script>
<script type="text/javascript" src="File2.js"></script>
</head>
<body>
<button onclick="DoStuff()">Do stuff</button>
</body>
</html>

我们有两个 js 包含文件,File1.js

    function ULSabc() { var o = new Object; o.File = "File1.js"; return o; }
/* ULSabc is the unique label for this js file. Each function in
this file can be decorated with a label corresponding with the same name */

function DoStuff() {
ULSabc: ;
//label matches name of function above
DoMoreStuff();
}

和 File2.js

    function ULSdef() { var o = new Object; o.File = "File2.js"; return o; }

function DoMoreStuff() {
ULSdef: ;
DoEvenMoreStuff();
}

function DoEvenMoreStuff() {
ULSdef: ;
try {
//throw an error
throw "Testing";
} catch (e) {
//handle the error by displaying the callstack
DisplayCallStack(e);
}
}

现在,假设我们的 ErrorHandling 文件如下所示

    function GetFunctionInfo(fn) {
var info = "";
if (fn) {
//if we have a function, convert it to a string
var fnTxt = fn.toString();

//find the name of the function by removing the 'function' and ()
var fnName = fnTxt.substring(0, fnTxt.indexOf("(")).substring(8);
info += "Function: " + fnName;

//next use a regular expression to find a match for 'ULS???:'
//which is the label within the function
var match = fnTxt.match(/ULS[^\s;]*:/);
if (match) {
var ULSLabel = match[0];

//if our function definition contains a label, strip off the
// : and add () to make it into a function we can call eval on
ULSLabel = ULSLabel.substring(0, ULSLabel.length - 1) + "()";

//eval our function that is defined at the top of our js file
var fileInfo = eval(ULSLabel);
if (fileInfo && fileInfo.File) {
//add the .File property of the returned object to the info
info += " => Script file: " + fileInfo.File;
}
}
}
return info;
}

function DisplayCallStack(e) {
//first get a reference to the function that call this
var caller = DisplayCallStack.caller;
var stack = "Error! " + e + "\r\n";

//recursively loop through the caller of each function,
//collecting the function name and script file as we go
while (caller) {
stack += GetFunctionInfo(caller) + "\r\n";
caller = caller.caller;
}

//alert the callstack, but we could alternately do something
//else like send the info to the server via XmlHttp.
alert(stack);
}

当我们单击页面上的按钮时,我们的脚本文件将调用每个函数并在 DisplayCallStack 处结束,此时它将递归循环并收集堆栈跟踪

    Error! Testing
Function: DoEvenMoreStuff => Script file: File2.js
Function: DoMoreStuff => Script file: File2.js
Function: DoStuff => Script file: File1.js
Function: onclick

关于javascript - 这段 JavaScript 代码有什么作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4177001/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com