AutoCAD 3DMAX C语言 Pro/E UG JAVA编程 PHP编程 Maya动画 Matlab应用 Android
Photoshop Word Excel flash VB编程 VC编程 Coreldraw SolidWorks A Designer Unity3D
 首页 > JavaScript

ECMAScript引用类型精讲

51自学网 http://www.51zixue.net

 

以下是引用片段:
    alert(o.toFixed( 2 )); // 输出15.00
  alert(o.toExponential( 1 )); // 输出1.5e+1
  在无法确定使用 toFixed 还是 toExponential 的时候,可以使用 toPrecision 方法来获得取值:

 

以下是引用片段:
  alert(o.toPrecision( 1 )); // 输出 2e+1
  alert(o.toPrecision( 2 )); // 输出 15
  alert(o.toPrecision( 3 )); // 输出 15.0
  String类

  String类是一种复杂引用类型,这里仅列出一些常见的方法,其中不少都是模仿java.lang.String:

  

以下是引用片段:
var s = new String( " Good Morning " );
  alert(s.valueOf() == s.toString()); // 输出true
  alert(s.length); // 输出12
  alert(s.charAt( 1 )); // 输出o
  var sr = s.concat( " ! " ); alert(sr); // 输出Good morning !
  alert(s.indexOf( " o " ); // 输出1
  alert(s.lastIndexOf( " o " ); // 输出6
  alert(s.localeCompare(Good morning)); // 输出0
  alert(s.localeCompare(Apple)); // 输出1
  alert(s.localeCompare(House)); // 输出-1
  alert(s.slice( 2 )); // 输出od morning
  alert(s.substring( 2 )); // 输出od morning
  alert(s.slice( 2 , - 5 )); // 输出od mo
  alert(s.substring( 2 , - 5 )); // 输出Go
  alert(s.toUpperCase()); // 输出GOOD MORNING
  alert(s.toLowerCase()); // 输出good morning
  另外,所有String类的方法同样可以用于String原始数据类型,因为它是伪对象。

  instanceof

  instanceof操作符和typeof作用类似,不同的是,instanceof需要明确指定对象是否属于某种特定类型。例如

以下是引用片段:
  var s = new String( " Good morning ! " );
  alert(s instanceof String);

 
 

上一篇:ECMAScript类型转换详解  下一篇:ECMAScript引用类型应用