El typeofoperador realmente no nos ayuda a encontrar el tipo real de un objeto.
Ya he visto el siguiente código:
Object.prototype.toString.apply(t)
Pregunta:
¿Es la forma más precisa de verificar el tipo de objeto?
El typeofoperador realmente no nos ayuda a encontrar el tipo real de un objeto.
Ya he visto el siguiente código:
Object.prototype.toString.apply(t)
Pregunta:
¿Es la forma más precisa de verificar el tipo de objeto?
Respuestas:
La especificación de JavaScript proporciona exactamente una forma adecuada de determinar la clase de un objeto:
Object.prototype.toString.call(t);
Object.prototype.toString.call(new FormData()) === "[object FormData]"que sería cierto. También puede usar slice(8, -1)para regresar en FormDatalugar de[object FormData]
Object.prototypey {}?
Object.prototype.toString.call(new MyCustomObject())vuelve [object Object]mientras new MyCustomObject() instanceOf MyCustomObject returns trueque es lo que quería (Chrome 54.0.2840.99 m)
new MyCustomObject().constructor === MyCustomObject.
el Object.prototype.toStringes una manera buena, pero su rendimiento es el peor.
http://jsperf.com/check-js-type

Use typeofpara resolver algún problema básico (String, Number, Boolean ...) y use Object.prototype.toStringpara resolver algo complejo (como Array, Date, RegExp).
y esta es mi solución:
var type = (function(global) {
var cache = {};
return function(obj) {
var key;
return obj === null ? 'null' // null
: obj === global ? 'global' // window in browser or global in nodejs
: (key = typeof obj) !== 'object' ? key // basic: string, boolean, number, undefined, function
: obj.nodeType ? 'object' // DOM element
: cache[key = ({}).toString.call(obj)] // cached. date, regexp, error, object, array, math
|| (cache[key] = key.slice(8, -1).toLowerCase()); // get XXXX from [object XXXX], and cache it
};
}(this));
usar como:
type(function(){}); // -> "function"
type([1, 2, 3]); // -> "array"
type(new Date()); // -> "date"
type({}); // -> "object"
typefunción es buena, pero observe cómo funciona en comparación con otras typefunciones. http://jsperf.com/code-type-test-a-test
La respuesta aceptada es correcta, pero me gusta definir esta pequeña utilidad en la mayoría de los proyectos que construyo.
var types = {
'get': function(prop) {
return Object.prototype.toString.call(prop);
},
'null': '[object Null]',
'object': '[object Object]',
'array': '[object Array]',
'string': '[object String]',
'boolean': '[object Boolean]',
'number': '[object Number]',
'date': '[object Date]',
}
Usado así:
if(types.get(prop) == types.number) {
}
Si está usando angular, incluso puede inyectarlo limpiamente:
angular.constant('types', types);
var o = ...
var proto = Object.getPrototypeOf(o);
proto === SomeThing;
Controle el prototipo que espera que tenga el objeto y luego compárelo con él.
por ejemplo
var o = "someString";
var proto = Object.getPrototypeOf(o);
proto === String.prototype; // true
o instanceof String; //true?
"foo" instanceof Stringrompe
typeof(x)==='string'úsalas.
Object.getPrototypeOf(true)falla donde (true).constructorvuelve Boolean.
Yo diría que la mayoría de las soluciones que se muestran aquí sufren de ingeniería excesiva. Probablemente la forma más simple de verificar si un valor es de tipo [object Object]es verificar la .constructorpropiedad del mismo:
function isObject (a) { return a != null && a.constructor === Object; }
o incluso más corto con funciones de flecha:
const isObject = a => a != null && a.constructor === Object;
La a != nullparte es necesaria porque uno puede pasar nullo undefinedno puede extraer una propiedad de constructor de ninguno de estos.
Funciona con cualquier objeto creado a través de:
Objectconstructor{}Otra característica interesante es su capacidad de proporcionar informes correctos para las clases personalizadas que utilizan Symbol.toStringTag. Por ejemplo:
class MimicObject {
get [Symbol.toStringTag]() {
return 'Object';
}
}
El problema aquí es que cuando se llama Object.prototype.toStringa una instancia, [object Object]se devolverá el informe falso :
let fakeObj = new MimicObject();
Object.prototype.toString.call(fakeObj); // -> [object Object]
Pero verificar contra el constructor da un resultado correcto:
let fakeObj = new MimicObject();
fakeObj.constructor === Object; // -> false
La mejor manera de averiguar el tipo REAL de un objeto (incluyendo AMBOS el nombre del objeto nativo o del tipo de datos (como String, Date, Number, ..etc) Y el tipo REAL de un objeto (incluso los personalizados); es agarrando La propiedad de nombre del constructor del prototipo de objeto:
Tipo nativo Ex1:
var string1 = "Test";
console.log(string1.__proto__.constructor.name);
muestra:
String
Ex2:
var array1 = [];
console.log(array1.__proto__.constructor.name);
muestra:
Array
Clases personalizadas:
function CustomClass(){
console.log("Custom Class Object Created!");
}
var custom1 = new CustomClass();
console.log(custom1.__proto__.constructor.name);
muestra:
CustomClass
nullo undefined.
Antigua pregunta que sé. No necesitas convertirlo. Ver esta función:
function getType( oObj )
{
if( typeof oObj === "object" )
{
return ( oObj === null )?'Null':
// Check if it is an alien object, for example created as {world:'hello'}
( typeof oObj.constructor !== "function" )?'Object':
// else return object name (string)
oObj.constructor.name;
}
// Test simple types (not constructed types)
return ( typeof oObj === "boolean")?'Boolean':
( typeof oObj === "number")?'Number':
( typeof oObj === "string")?'String':
( typeof oObj === "function")?'Function':false;
};
Ejemplos:
function MyObject() {}; // Just for example
console.log( getType( new String( "hello ") )); // String
console.log( getType( new Function() ); // Function
console.log( getType( {} )); // Object
console.log( getType( [] )); // Array
console.log( getType( new MyObject() )); // MyObject
var bTest = false,
uAny, // Is undefined
fTest function() {};
// Non constructed standard types
console.log( getType( bTest )); // Boolean
console.log( getType( 1.00 )); // Number
console.log( getType( 2000 )); // Number
console.log( getType( 'hello' )); // String
console.log( getType( "hello" )); // String
console.log( getType( fTest )); // Function
console.log( getType( uAny )); // false, cannot produce
// a string
Bajo costo y simple.
falsesi el objeto de prueba es nulloundefined
trueofalse
false. ¿Cómo ayuda esto a responder la pregunta?
Reuní una pequeña utilidad de verificación de tipo inspirada en las respuestas correctas anteriores:
thetypeof = function(name) {
let obj = {};
obj.object = 'object Object'
obj.array = 'object Array'
obj.string = 'object String'
obj.boolean = 'object Boolean'
obj.number = 'object Number'
obj.type = Object.prototype.toString.call(name).slice(1, -1)
obj.name = Object.prototype.toString.call(name).slice(8, -1)
obj.is = (ofType) => {
ofType = ofType.toLowerCase();
return (obj.type === obj[ofType])? true: false
}
obj.isnt = (ofType) => {
ofType = ofType.toLowerCase();
return (obj.type !== obj[ofType])? true: false
}
obj.error = (ofType) => {
throw new TypeError(`The type of ${name} is ${obj.name}: `
+`it should be of type ${ofType}`)
}
return obj;
};
ejemplo:
if (thetypeof(prop).isnt('String')) thetypeof(prop).error('String')
if (thetypeof(prop).is('Number')) // do something
nullo undefinedo trueofalse