-
Original : tipo MDN
-
Función : el
typeof
operador devuelve una cadena que indica el tipo de operando no evaluado. -
Método :
typeof operand
otypeof(operand)
operand
: Una expresión que representa un objeto o valor primitivo, cuyo tipo se devolverá.
-
Descripción :
Estos son los typeof
posibles valores devueltos:
Tipos de | resultado |
---|---|
Indefinido | 'indefinido' |
Nulo | 'objeto' |
Booleano | 'boolead' |
Número | 'número' |
Empezando | 'Empezando' |
Cuerda | 'cuerda' |
Símbolo | 'símbolo' |
Función | 'función' |
Cualquier otro objeto | 'objeto' |
- Codigo :
/**
* @name typeof测试
* @description 通过 typeof 检测各个数据类型的返回
*/
const test = {
testUndefined: undefined,
testNull: null,
testBoolean: true,
testNumber: 123,
testBigInt: BigInt(1234), // 大于 2 的 53 次方算 BigInt
testString: '123',
testSymbol: Symbol(),
testFunction: function() {
console.log('function');
},
testObject: {
obj: 'yes',
},
testObjectString: new String('String'),
testObjectNumber: new Number(123),
}
console.log(typeof(test.testUndefined)); // undefined
console.log(typeof(test.testNull)); // object
console.log(typeof(test.testBoolean)); // boolean
console.log(typeof(test.testNumber)); // number
console.log(typeof(test.testBigInt)); // bigint
console.log(typeof(test.testString)); // string
console.log(typeof(test.testSymbol)); // symbol
console.log(typeof(test.testFunction)); // function
console.log(typeof(test.testObject)); // object
console.log(typeof(test.testObjectString)); // object
console.log(typeof(test.testObjectNumber)); // object