學習啦 > 知識大全 > 知識百科 > 百科知識 > js中構造函數是什么

js中構造函數是什么

時間: 歐東艷656 分享

js中構造函數是什么

  構造函數(constructor)是一種特殊的方法 。主要用來在創(chuàng)建對象時初始化對象, 即為對象成員變量賦初始值,總與new運算符一起使用在創(chuàng)建對象的語句中 。特別的一個類可以有多個構造函數 ,可根據其參數個數的不同或參數類型的不同來區(qū)分它們 即構造函數的重載。構造函數的功能主要用于在類的對象創(chuàng)建時定義初始化的狀態(tài)。


構造函數

  javascript實例

  在本例中,我們將展示如何使用 constructor 屬性。

  vartest=newBoolean();

  if(test.constructor==Array)

  {

  document.write("ThisisanArray");

  }

  if(test.constructor==Boolean)

  {

  document.write("ThisisaBoolean");

  }

  if(test.constructor==Date)

  {

  document.write("ThisisaDate");

  }

  if(test.constructor==String)

  {

  document.write("ThisisaString");

  }

246707