Proprietà constructor

Specifica la funzione che crea un oggetto.

object.constructor

Argomenti

  • object
    Obbligatoria. 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)
        return ("Object is a String.");
    else if (ob.constructor == MyFunc)
        return ("Object is constructed from MyFunc.");
    else
        return ("Object is neither a String nor constructed from MyFunc.");
}

// A constructor function.
function MyFunc() {
    // Body of function.
}

var x = new String("Hi");
print(testObject(x));

var y = new MyFunc;
print(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