Condividi tramite


Proprietà constructor

Aggiornamento: novembre 2007

Specifica la funzione che crea un oggetto.

object.constructor

Argomenti

  • object
    Obbligatorio. Nome di un oggetto o di una funzione.

Note

La proprietà constructor è un membro del prototipo di ogni oggetto che dispone di un prototipo. Sono inclusi tutti gli oggetti JScript intrinseci a eccezione degli oggetti arguments, Enumerator, Error, Global, Math, RegExp, Regular Expression e VBArray. La proprietà constructor include un riferimento alla funzione che costruisce le istanze dell'oggetto specifico.

Gli oggetti basati su classi non dispongono di un metodo constructor.

Esempio

Nell'esempio seguente viene illustrato l'utilizzo della proprietà constructor.

function testObject(ob) {
   if (ob.constructor == String)
      print("Object is a String.");
   else if (ob.constructor == MyFunc)
      print("Object is constructed from MyFunc.");
   else
      print("Object is neither a String or constructed from MyFunc.");
}
// A constructor function.
function MyFunc() {
   // Body of function.
}

var x = new String("Hi");
testObject(x)
var y = new MyFunc;
testObject(y);

L'output del programma è il seguente:

Object is a String.
Object is constructed from MyFunc.

Requisiti

Versione 2

Si applica a:

Oggetto Array| Oggetto Boolean| Oggetto Date| Oggetto Function| Oggetto Number| Oggetto Object| Oggetto String

Vedere anche

Riferimenti

Proprietà prototype