JavaScript 对象

  1. Array 对象
  2. Date 对象
  3. Error 对象
  4. Math 对象
  5. String 对象
  6. Enumerator 对象
  7. ActiveXObject 对象

ActiveXObject 生成应用程序对象,语法:

newObj = new ActiveXObject("servername.typename" [,location])
参数 意义
servername 应用程序名称
typename 对象的类或类型
location 网络服务器名称

示例:

// 打开 WORD 应用程序。

word = new ActiveXObject("Word.Application")

word.visible = true

// 打开 EXCEL 应用程序,打开一个表格,写入数据,最后保存关闭

 var ExcelApp, ExcelSheet;
 ExcelApp = new ActiveXObject("Excel.Application");
 ExcelSheet = ExcelApp.Workbooks.Open("C:\\windows\\desktop\\ab.xls");
 ExcelSheet.Application.Visible = true;
 ExcelSheet.ActiveSheet.Cells(1,1).Value = "This is column A, row 1";
 ExcelSheet.Save();
 ExcelSheet.Application.Quit();

对象共有方法/属性:

方法/属性 意义
constructor 创建对象的函数
prototype 对象名称,引用对象原型
valueOf() 返回对象的原始值
toString(...) 返回字符串形式

检测对象属性的方法

if ("prop" in Object)

// 有 prop 属性

constructor 示例:

此示例判断变量 x 建立时是否使用 String/MyFunc 函数。

x = new String("Hi");

if( x.constructor == String )

   ...

 

function MyFunc {

 ...

}

y = new MyFunc;

if( y.constructor == MyFunc )

   ...

prototype 示例:

通过 prototype 属性添加自定义成员函数。

function array_max () {

   var i,max = this[0];

   for (i=1;i<this.length;i++) {

      if ( max<this[i] ) max = this[i];

   }

   return max;

}

Array.prototype.max = array_max;

var x = new Array(1,2,3,4,5,6);

var y = x.max();< /p >