📄 function.js
字号:
/*
* Isomorphic SmartClient
* Version 6.5 (2008-04-30)
* Copyright(c) 1998-2007 Isomorphic Software, Inc. All rights reserved.
* "SmartClient" is a trademark of Isomorphic Software, Inc.
*
* licensing@smartclient.com
*
* http://smartclient.com/license
*/
//>DEBUG// This lets us label methods with a name within addMethodsFunction.prototype.Class = "Function"; //<DEBUG //> @class Func// Class of utility methods for exploring and manipulating functions and methods// @treeLocation Client Reference/System//<isc.ClassFactory.defineClass("Func");isc.Func.addClassMethods({ //> @method Func.getName() (A) // // Gets the name of the function as a string. // @param func (function) Function to get the name for // @return (string) name of the function // //< // create the static regular expression we use to quickly extract the name of a function _nameExpression : new RegExp("function\\s+([\\w$]+)\\s*\\("), getName : function (func, dontReport) { if (func == Function.prototype.apply) return "Function.apply"; if (func == Function.prototype.call) return "Function.call"; // if we've previously determined our name or been explicitly labelled with a name, return // that if (func._fullName == null) { if (func._className == null && isc._allFuncs) { var index = isc._allFuncs.indexOf(func); if (index != -1) { for (var className = isc._funcClasses[index]; className == null; index--) { className = isc._funcClasses[index]; } func._className = className; } } // if we have a className but no function name, search the class (and instance // prototype) for the function var name = func._name; if (name == null && func._className != null) { var theProto; var classObj = isc.ClassFactory.getClass(func._className); if (classObj == null) { // support lookups for non-Class singletons like isc.ClassFactory and // isc.FileLoader, and native globals like Array and Function //Log.logWarn("className is: " + func._className); classObj = isc[func._className] || window[func._className]; } else { theProto = classObj.getPrototype(); } // check instance methods first (more common) if (theProto != null) { for (var methodName in theProto) { if (theProto[methodName] === func) { name = methodName; break; } } } // then class methods if (name == null && classObj != null) { for (var methodName in classObj) { if (classObj[methodName] === func) { name = methodName; break; } } // if this is a native object, check the prototype methods as well if (name == null && !isc.isA.Class(classObj) && classObj.prototype != null) { for (var methodName in classObj.prototype) { if (classObj.prototype[methodName] === func) { name = methodName; break; } } } } } if (name != null) { func._fullName = (func._instanceSpecific ? (func._isOverride ? "[o]" : "[a]") : isc._emptyString) + (func._className ? func._className + isc.dot : isc._emptyString) + name; } else { if (func._isCallback) func._fullName = "callback"; else { // derive the name from the function definition using a regular expression var match = isc.Func._nameExpression.exec(func.toString()); if (match) func._fullName = match[1]; // if the regex didn't match, // it's an anonymous function // NOTE that new Function().toString() is "function anonymous() { }" on // both Moz and IE else func._fullName = "anonymous"; } } } return func._fullName; }, //> @method Func.getArgs() (A) // // Gets the arguments to the function as an array of strings // // @param func (function) Function to examine // @return (array) argument names for the function (array of strings) // returns an empty array if the function has no arguments. //< getArgs : function (func) { var args = isc.Func.getArgString(func); if (args == "") return []; return args.split(","); }, //> @method Func.getArgString() (A) // // Gets the arguments to the function as a string of comma separated values // // @param func (function) Function to examine // @return (string) argument names for the function separated by commas // returns an empty string if the function has no arguments. //< getArgString : function (func) { var string = func.toString(), args = string.substring(string.indexOf("(") + 1, string.indexOf(")")); return args; }, //> @method Func.getBody() (A) // // Gets the body of the function as a string.<br><br> // // NOTE: This is the body of the function after it has been parsed -- all comments will // have been removed, formatting may be changed from the original text, etc. // // @param func (function) function to examine // @return (strings) body of the function as a string, without leading "{" and trailing "}" //< getBody : function (func) { var string = func.toString(); return string.substring(string.indexOf("{") + 1, string.lastIndexOf("}")); }, //> @method Func.getShortBody() (A) // // Gets the body of the function as a string, removing all returns so it's more // compact.<br><br> // // NOTE: This is the body of the function after it has been parsed -- all comments will // have been removed, formatting may be changed from the original text, etc. // // @param func (function) function to examine // @return (string) body of the function as a string, without leading "{" and trailing "}" //< getShortBody : function (func) { var string = func.toString(); return string.substring(string.indexOf("{") + 1, string.lastIndexOf("}")).replace(/[\r\n\t]*/g, ""); }});// function.apply()// This is a native method in most browsers.// If it's not already defined, supply the "apply" function.// If it is already defined, patch it so it will not JS error if explicitly passed// <code>null</code> as the arguments (2nd) parameter.//> @method function.apply() (A)//// Applies this function to <code>targetObject</code>, as if the function was originally// defined as a method of the object.//// @param targetObject (object) target to apply the function to. Within the context// of the function as it evaluates, <code>this</code> == <code>targetObject</code>// @param args (array of objects) list of arguments to pass to the function//// @return (varies) returns the normal return value of the function//<if (isc.Browser.isSafari || !Function.prototype.apply) { // temporary function number for generating a new function name isc.addMethods(Function.prototype, { apply: function (targetObject, args) { //!DONTOBFUSCATE // generate a temporary function name var tempFunctionName = "__TEMPF_" + Function.prototype._tempFuncNum++; var returnValue; // assign the function being apply'd (this) to the targetObject targetObject[tempFunctionName]=this; // if no argments passed, set args to an empty array if (!args) args = []; if (args.length <= 10) { // Note any undefined properties of the args array will simply be // undefined arguments of the function being invoked via apply, as // they should be. The arguments.length of the function will be off, but so be it returnValue = targetObject[tempFunctionName](args[0],args[1],args[2],args[3],args[4], args[5],args[6],args[7],args[8],args[9]); } else { // The function is being called with more than ten arguments. // Construct a string with the code necessary to call the function with // however many arguments were passed, then eval() it. var functionString = 'targetObject[tempFunctionName]('; for (var i = 0; i < args.length; i++) { functionString += "args" + '[' + i + ']'; if (i + 1 < args.length) { functionString += ','; } } functionString += ');'; eval('returnValue =' + functionString); } // remove the temporary function from the targetObject delete targetObject[tempFunctionName]; // and return the value returned by the function call return returnValue; } }); // counter which is used to generate unique names for functions to be applied Function.prototype._tempFuncNum = 0;} // Add some static helper methods to the Func classisc.Func.addClassMethods({ // Helper properties _commentDelimeters : [["//", "\n"], ["//", "\\n"], ["/*", "*/"]], _stringDelimeters : ["\"", "\'"], _complexIdentifiers : ["switch", "while", "if", "return", "for", "var"], _multiLineDelimeters : ["(", ")", "[", "]", "{", "}", ":", "?", "!", "+", "-", "/", "*", "=", ">", "<","|", "&", ",", "\\"], //> @method isc.Func.expressionToFunction() (A) // // Given an expression or conditional as a string, convert it into // a Function object. Used to create functions that need to return // values where the user specifies a string. These were formerly done // via evals. // // @params variables (string) Names of variables to pass into the new function // @params expression (string) String expression to evaluate return // // @return (function) function that returns the conditional value //< expressionToFunction : function (variables, expression, comment) { var returnValue = this._expressionToFunction(variables, expression, comment); return returnValue; }, _expressionToFunction : function (variables, expression, comment) { if (expression == null) { //>DEBUG isc.Log.logInfo("makeFunctionExpression() called with empty expression"); //<DEBUG expression = ""; } // Handle being passed an action type object. // This is an object of the format // { target:"componentId", name:"fetchData", title:"click" } // or // { target: "someForm", name : "editRecord", title:"itemChanged", // // action method param name -> expression to populate it // mapping : { // record : "record", // callback : "someExpression()" // something use manually entered // } // } if (isc.isAn.Object(expression)) { if (isc.isA.StringMethod(expression)) expression = expression.getValue(); var varsArray = variables; if (isc.isA.String(varsArray)) varsArray= variables.split(","); else if (isc.isAn.Array(varsArray)) { variables = varsArray.join(); } if (!isc.isAn.Array(varsArray)) varsArray = []; var expressionArray = [ // Warn if we can't find the target "if (!window.", // 0 , // 1 (ID of target) "){var message='Component ID \"", // 2 , // 3 (ID of target) "\", target of action \"", // 4 , // 5 (action title) "\" does not exist';isc.Log.logWarn(message);if(isc.designTime)isc.say(message);}", // 6 // Call the method on the target , // 7 target ID ".", // 8 , // 9 method name "(", // 10 , // 11 arguments [as a ',' separated string] ")" // then close with ")" ]; // Plug the ID of the target, and the method to call into the function string. expressionArray[1] = expressionArray[3] = expressionArray[7] = expression.target; expressionArray[9] = expression.name; if (expression.title) expressionArray[5] = expression.title; else expressionArray[5] = "[No title specified]" // mapping is an array of expressions to pass in as parameters var mapping = expression.mapping || []; if (!isc.isAn.Array(mapping)) mapping = []; expressionArray[11] = mapping.join(); // automatically puts commas between args
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -