Microsoft AJAX Library Cheat Sheet(5): Number和Error类型的扩展
本文为翻译,英文原版的Cheat Sheet(PDF版本)在此下载:http://aspnetresources.com/downloads/ms_ajax_library_cheat_sheets1.zip
原作版权声明:
Copyright (c) 2004-2006, Milan Negovan http://www.AspNetResources.com All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * The name of the author may not be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
注:标注有[S]的为静态方法,无须实例化对象即可使用。
Number.format (format)
用指定的format格式化数字(区域设定无关)。
Number.localeFormat (format)
用指定的format格式化数字(区域设定相关)。
支持的格式
- p:将数字转为百分比字符串(e.g.: -1,234.56 %)
- d:将数字装化为十进制字符串,没有逗号分隔符(e.g.: -1234.56)
- c:将数字转化为货币金额形式(e.g.: (¤1,234.56))
- n:将数字串化为逗号三位分隔(-d,ddd,ddd.ddd…")形式(e.g.: -1,234.56)
[S] Number.parseLocale (value)
从字符串表示的本地数字解析成相应的Number类型。使用Sys.CultureInfo.CurrentCulture得到当前的区域属性。
[S] Number.parseInvariant (value)
从字符串表示的本地数字解析成相应的浮点数Number类型。value中可以包含逗号分隔符(,)或是正负(+-)号。
var a = new Number();
a = Number.parseInvariant("4");
var b = new Number(2);
var c = Number.parseInvariant("1.53") + a + b;
// c = 7.53
Error相关的方法
- [S] Error.argument:根据指定的异常信息和非法的参数创建一个Sys.ArgumentException类型的异常。
- [S] Error.argumentNull:根据指定的异常信息和为Null的参数创建一个Sys.ArgumentNullException 类型的异常。
- [S] Error.argumentType:根据指定的异常信息、期待参数类型和实际参数类型创建一个Sys.ArgumentTypeException 类型的异常。
- [S] Error.argumentUndefined:根据指定的异常信息和为定义的参数创建一个Sys.ArgumentUndefinedException类型的异常。
- [S] Error.create:根据指定的异常信息创建一个Error对象。
- [S] Error.invalidOperation:根据指定的异常信息和引发该异常的参数创建一个Sys.InvalidOperationException 类型的异常。
- [S] Error.notImplemented:根据指定的异常信息创建一个Sys.NotImplementedException类型的异常。
- [S] Error.argumentOutOfRange:根据指定的异常信息和引发该异常的参数创建一个Sys.ArgumentOutOfRangeException类型的异常。
- [S] Error.parameterCount:根据指定的异常信息创建一个Sys.ParameterCountException 类型的异常。
- Error.popstackFrame:将该Error实例的fileName和lineNumber字段更新为该Error被掷出时的位置,而不是该Error被创建时的位置。
// Throw a standard exception type
var err = Error.argumentNull("input", "A parameter was undefined.");
throw err;
// Throw a generic error with a message and associated errorInfo object.
var errorInfoObj = { name: "SomeNamespace.SomeExceptionName",
someErrorID: "436" };
var err = Error.create("A test message", errorInfoObj);
throw err;