typeof在javascript中的用法是什么?

typeof在javascript中的用法是什么?,javascript,typeof,Javascript,Typeof,typeof返回原始数据类型,但我不明白为什么在javascript中使用它 我不明白为什么在javascript中使用它 typeof用于 返回原始数据 例如,如果我想知道某件事是否未定义,我可以这样做 if (typeof object === 'undefined') 检查,因为如果未定义,则没有数据类型(因为未定义)。这就是为什么会使用typeof而不是出于日志记录的目的,以查看通过ajax接收到的内容,或者使函数接受可以具有不同类型的参数,并使用typeof检查该类型,等。typeo

typeof
返回原始数据类型,但我不明白为什么在javascript中使用它

我不明白为什么在javascript中使用它

typeof
用于

返回原始数据

例如,如果我想知道某件事是否未定义,我可以这样做

if (typeof object === 'undefined')

检查,因为如果未定义,则没有数据类型(因为未定义)。这就是为什么会使用
typeof
而不是出于日志记录的目的,以查看通过ajax接收到的内容,或者使函数接受可以具有不同类型的参数,并使用
typeof
检查该类型,等。

typeof是一个一元运算符,放在可以是任何类型的单个操作数之前。其值是指定操作数类型的字符串

   - x                             typeof x

   undefined                        "undefined"
   null                             "object"
   true or false                    "boolean"
   any number or NaN                "number"
   any string                       "string"
   any function                     "function"
   any non function native object   "object"
typeof可以很好地处理除null以外的基本值。typeof无法区分null和object,因为null是虚假的,objects是真实的。下面是一些可能有用的方法。typeof对除函数以外的所有对象和数组值求值为object。如何处理函数可能超出了这个问题的范围


希望这能对您有所帮助。

您可以使用JavaScript
typeof
操作符查找JavaScript变量的类型。它还用于验证变量或输入。更好地解释=> 示例 typeof“John”//Returns string typeof 3.14//Returns number typeof false//Returns boolean typeof[1,2,3,4]//返回对象类型{name:'John',age:34}//Returns object

javascript中的typeof为什么使用它

有时您可能需要检查变量中存储的数据类型,例如,typeof是一个运算符而不是一个函数,它与停止函数执行并从该函数返回值的return语句完全不同

typeof运算符返回一个字符串,指示未赋值操作数的类型

console.log(typeof 'name');
// expected output: "string"
console.log(typeof 74);
// expected output: "number"
console.log(typeof true);
// expected output: "boolean"
console.log(typeof declaredButUndefinedVariable);
// expected output: "undefined";

typeof运算符后面始终跟有其操作数:

typeof UnaryExpression=>请记住,括号是可选的,但请记住:括号对于确定表达式的数据类型非常有用

var data = 100;
typeof data + ' Bye'; // return 'number Bye'
typeof (data + 'Bye'); // return 'string'
其他例子:

// Numbers
typeof 37 === 'number';
typeof 3.14 === 'number';
typeof(42) === 'number';
typeof Math.LN2 === 'number';
typeof Infinity === 'number';
typeof NaN === 'number'; // Despite being "Not-A-Number"
typeof Number('1') === 'number'; // Number tries to parse things into numbers


// Strings
typeof '' === 'string';
typeof 'bla' === 'string';
typeof `template literal` === 'string';
typeof '1' === 'string'; // note that a number within a string is still typeof string
typeof (typeof 1) === 'string'; // typeof always returns a string
typeof String(1) === 'string'; // String converts anything into a string, safer than toString


// Booleans
typeof true === 'boolean';
typeof false === 'boolean';
typeof Boolean(1) === 'boolean'; // Boolean will convert values based on if they're truthy or falsy, equivalent to !!


// Symbols
typeof Symbol() === 'symbol'
typeof Symbol('foo') === 'symbol'
typeof Symbol.iterator === 'symbol'


// Undefined
typeof undefined === 'undefined';
typeof declaredButUndefinedVariable === 'undefined';
typeof undeclaredVariable === 'undefined'; 


// Objects
typeof {a: 1} === 'object';

// use Array.isArray or Object.prototype.toString.call
// to differentiate regular objects from arrays
typeof [1, 2, 4] === 'object';

typeof new Date() === 'object';
typeof /regex/ === 'object'; // See Regular expressions section for historical results

以下是一种常见的有点问题的黑客行为:

const type = obj => Object.prototype.toString.call(obj);

type("abc");// [object String]
type(123);// [object Number]// What's with all the objects?
type([]);// [object Array]
type({});// [object Object]
type(Object.create(null));// [object Object]
type(-1/0);// [object Number] Not exactly a true number
type(NaN);// [object Number] WTF?
正如你所看到的,它有一些问题。它总是返回两个用括号括起来的类型,第一个总是一个“对象”。如果总是返回第一种类型的信息,则它将变得无用。其次,它的区别是有限的。它无法告诉我们对象是作为文本(普通)创建的,还是使用object.create()创建的,调用时需要关键字“new”。它还错误地将无穷大和NaN称为一个数字

我希望与大家分享一个更好的函数类型,它可以解决所有这些问题。它适用于所有原语,包括符号、异常(错误、未定义、null和NaN)、本机对象和函数的最常见情况(数组、映射、对象、函数、数学、日期、承诺等),甚至可以检测用户创建的对象(标识为普通)和DOM元素(标识为HTML)之间的差异。它应该适用于所有现代和稍旧的浏览器。它被分解为几个函数,以使代码更易于使用:

const isDOM = obj => (obj.nodeType && !isPlain(obj)) ? true : false;
const isPlain = obj => obj ? obj.constructor === {}.constructor : false;
const sanString = str => str.replace(/[^a-zA-Z ]/g, "");
const isNaN = obj => (Object.is(obj, NaN) === true) ? true : false;

function objType(obj){
if(obj === undefined) return undefined;
if(obj === Infinity) return Infinity;
if(obj === -Infinity) return -Infinity;
if(isDOM(obj)) return 'HTML';
let str = Object.prototype.toString.call(obj);
if(str === '[object Object]' && isPlain(obj)) return 'Plain';
str = sanString(str).split(' ');
if(str[1] === 'Number' && isNaN(obj)) return NaN;
return str[1];}
}
这样使用:

objType(null);// Null
objType(undefined);// undefined
objType("abc");// String
objType(123);// Number
objType([]);// Array
objType({});// Plain not [object Object]
objType(Object.create(null));// Object is what we want
objType(document.body);// HTML
objType(-1/0);// -Infinity
objType(NaN);// NaN

请让我知道,如果你发现任何错误或错误或有更好的解决方案(不是从图书馆或freameworks)。我将很乐意修复它。

运算符
typeof
返回一个表示操作数类型的字符串。我们可以通过以下方式使用它来检查值的类型:

设nr=5;
如果(nr的类型=='number'){
console.log('nr为编号');
}
设str='hi';
if(typeof str=='string'){
log('str是string');

}
它用于检索数据类型。有时,在处理某个值之前,您想知道该值是什么类型。另一个原因是当您让函数接受不同的参数时
typeof
将允许您确定类型并相应地执行操作。最好这样做:如果(对象===未定义),则在尝试防止错误时将此条件置于范围的顶部。这是因为如果这是您的目标,if(typeof object===undefined)不一定会分支出来。+1因为这是一个实际的代码示例。所有其他示例都没有显示
if
语句中的基本用法…
objType({constructor:'',nodeType:true})
=>“HTML”。真正地我不太确定你想在这里做什么,但我看不出这是如何回答当前问题的。请注意,不要检查这样的属性,例如要知道对象是否是节点,请调用节点的
对象实例
。使用typeof(document.body)返回[object HTMLBodyElement],每个DOM元素都有自己的构造函数。我的对象类型将所有DOM分组为HTML。当您希望在操作DOM时检查代码,以确保它实际生成HTML元素时,这非常有用。。。所以首先,
typeof
是一个操作符,您不需要像调用函数一样调用它(不需要
()
)。然后我的第一点是,对于
{constructor:'',nodeType:true}
,函数将返回
'HTML'
,而它只是一个普通对象。这是因为您在检查属性时出错了。不管我怎么想你的代码在做什么(IMM,这只是一种复杂的出错和编写更多代码的方式),它绝对不能回答眼前的问题。