Microsoft AJAX Library Cheat Sheet(6):String和Object类型的扩展

本文为翻译,英文原版的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]的为静态方法,无须实例化对象即可使用。

 

String.endsWith (suffix)

判断该String是否以指定的后缀结束。

var isTxt = myString.endsWith ("some_file.txt", ".txt");
// isTxt = true

 

[S] String.format (format, args)

将该String中的各个格式化项用相应的参数值替代。args可以为单一的Object,或是一个包含多个项的Array。

var user = {
    FirstName: 'John',
    LastName : 'Doe',
    DOB: new Date (1975, 1, 17) };
var label = String.format ("User name: {0} {1}, born {2:d}",
    user.FirstName, user.LastName, user.DOB));
// label = "User name: John Doe, born 02/17/1975"

 

[S] String.localeFormat (format, args)

功能与String.format (format, args)类似,不过格式化时与区域设定相关。

 

String.startsWith (prefix)

判断该String是否以指定的前缀开始。请参考String.endsWith (suffix)。

 

String.trim ()

返回一个原String的拷贝,但将原String开头和结尾部分的所有空白字符(例如空格或Tab)都移除。

var myString = "   A string   ";
myString = myString.trim();
// myString = "A string"

 

String.trimEnd ()

返回一个原String的拷贝,但将原String结尾部分的所有空白字符(例如空格或Tab)都移除。

var myString = "A string   ";
myString = myString.trim();
// myString = "A string"

 

String.trimStart ()

返回一个原String的拷贝,但将原String开头部分的所有空白字符(例如空格或Tab)都移除。

var myString = "   A string";
myString = myString.trim();
// myString = "A string"

 

[S] Object.getType (instance)

返回指定对象的类型,请参考Object.getTypeName (instance)。

 

[S] Object.getTypeName (instance)

返回一个表示对象在运行时的完全限定类型的字符串。

Type.registerNamespace("Samples");
// Define and register a Samples.Rectangle class.
Samples.Rectangle = function(width, height) {
    this._width = width;
    this._height = height;
}
Samples.Rectangle.registerClass("Samples.Rectangle");
var a = new Samples.Rectangle(200, 100);
var name = Object.getTypeName(a);
var type = Object.getType (a);
Sys.Debug.trace ("The type name of object \"a\" is: " + name);
Sys.Debug.traceDump (type);
/* The result is:
    The type name of object "a" is: Samples.Rectangle
    traceDump {Function}
    prototype {Samples.Rectangle}
        __typeName: Samples.Rectangle
        __class: true
*/
posted on 2007-02-09 21:38  Dflying Chen  阅读(2815)  评论(0编辑  收藏  举报