Rob*_*nik 17 javascript sharepoint sharepoint-2010
我一直在看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();
}
Run Code Online (Sandbox Code Playgroud)
实际上,此文件中的每个函数定义都在ULSTYE:;左大括号后面的同一行开头.任何人都能解释第二个函数的第一行是做什么的吗?
例如,Firefox/Firebug将此功能解释为我无法理解的功能:
function () {
ULSTYE: {
}
return new (SP.UI.DialogOptions);
}
Run Code Online (Sandbox Code Playgroud)
我以为我一直都知道Javascript ...... ;)必须是我过去从未使用的一些模糊的功能,显然很少被其他人使用.
Pau*_*cas 21
经过长时间的疑惑,我终于坐下来解决了这个问题.它是用于在客户端上收集诊断信息的相对复杂的机制的一部分,其包括将javascript callstack(包括函数名称和javascript文件)发送回服务器的能力.
看看在第250行的文件init.debug.js的它位于
%Program Files%\ Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\LAYOUTS\1033\init.debug.js
该文件定义了客户端上"ULS"实现的所有功能.
当然,您需要安装SharePoint 2010才能使文件存在于本地计算机上.
更新 -以下概述了该机制的工作原理.真正的实现不仅仅是这个
考虑以下带有几个js包含的html页面,每个页面都可以相互调用.
<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>
Run Code Online (Sandbox Code Playgroud)
我们有两个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();
}
Run Code Online (Sandbox Code Playgroud)
和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);
}
}
Run Code Online (Sandbox Code Playgroud)
现在,假设我们的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);
}
Run Code Online (Sandbox Code Playgroud)
当我们单击页面上的按钮时,我们的脚本文件将调用每个函数并以DisplayCallStack结束,此时它将递归循环并收集堆栈跟踪
Error! Testing
Function: DoEvenMoreStuff => Script file: File2.js
Function: DoMoreStuff => Script file: File2.js
Function: DoStuff => Script file: File1.js
Function: onclick
Run Code Online (Sandbox Code Playgroud)
T.J*_*der 14
第一位定义了一个函数,该函数创建具有几个属性的对象并返回它.我想我们都清楚这一点.:-)
但是,第二位没有使用该功能.它定义了一个具有相同名称的标签.虽然它使用相同的字符序列,但它不是对上述函数的引用.Firefox的解释与其他任何东西一样有意义,因为标签后面应该引用它可以引用的东西.
有关标签语句的更多信息,请参阅规范的第12.12节.
偏离主题:我会避免使用此来源的代码.编写它的人显然对JavaScript很新,并没有表明他们知道自己在做什么.例如,他们已经离开了()关闭new Object()调用,虽然这是允许的,这是相当不可靠的事情.他们可以争辩说他们这样做是为了节省空间,但如果他们这样做,他们最好使用对象文字:
function ULSTYE() {
return {
ULSTeamName: "Microsoft SharePoint Foundation",
ULSFileName: "SP.UI.Dialog.debug.js"
};
}
Run Code Online (Sandbox Code Playgroud)
写作根本没有太多理由new Object(); {}在功能上是相同的.
当然,第二位根本就没有理由.:-)
| 归档时间: |
|
| 查看次数: |
2401 次 |
| 最近记录: |