Questions tagged «typeof»

Typeof(或者typeof或TypeOf)是由几种编程语言提供的运算符,用于确定给定变量的数据类型。当构造需要接受多种数据类型但可能需要根据提供的数据类型采取不同操作的程序部分时,这很有用。


14
类型检查:typeof,GetType还是?
我见过很多人使用以下代码: Type t = typeof(obj1); if (t == typeof(int)) // Some code here 但我知道您也可以这样做: if (obj1.GetType() == typeof(int)) // Some code here 或这个: if (obj1 is int) // Some code here 就个人而言,我觉得最后一个是最干净的,但是我缺少什么吗?哪一种是最佳使用方式,还是个人喜好?
1511 c#  types  typeof  gettype 





11
获取Javascript变量类型的更好方法?
是否有比JS中更好的方法来获取变量的类型typeof?当您这样做时,它可以正常工作: > typeof 1 "number" > typeof "hello" "string" 但是,当您尝试以下操作时,它是没有用的: > typeof [1,2] "object" >r = new RegExp(/./) /./ > typeof r "function" 我知道instanceof,但这需要您事先知道类型。 > [1,2] instanceof Array true > r instanceof RegExp true 有没有更好的办法?
260 javascript  types  typeof 

6
C ++中的'typeid'与'typeof'
我想知道C ++ typeid和typeofC ++ 之间的区别是什么。这是我所知道的: typeid是在C ++头文件typeinfo中定义的type_info文档中提到的 。 typeof在C的GCC扩展和C ++ Boost库中定义。 另外,这是我在发现typeid未返回预期结果的地方创建的测试代码test 。为什么? main.cpp #include <iostream> #include <typeinfo> //for 'typeid' to work class Person { public: // ... Person members ... virtual ~Person() {} }; class Employee : public Person { // ... Employee members ... }; int main () …
159 c++  typeof  typeid 

6
在C ++ 11中用“ auto”推论得出的lambda类型是什么?
我认为lambda的类型是函数指针。当我执行以下测试时,我发现它是错误的(演示)。 #define LAMBDA [] (int i) -> long { return 0; } int main () { long (*pFptr)(int) = LAMBDA; // ok auto pAuto = LAMBDA; // ok assert(typeid(pFptr) == typeid(pAuto)); // assertion fails ! } 上面的代码缺少任何意义吗?如果不是,那么typeof用auto关键字推导的lambda表达式是什么?
141 c++  lambda  c++11  typeof  auto 



6
获取所有变量的类型
在R中,我想在脚本末尾检索全局变量列表并对其进行迭代。这是我的代码 #declare a few sample variables a<-10 b<-"Hello world" c<-data.frame() #get all global variables in script and iterate over them myGlobals<-objects() for(i in myGlobals){ print(typeof(i)) #prints 'character' } 我的问题是,即使变量而且不是字符变量,也typeof(i)总是返回。如何在for循环中获取变量的原始类型?characterac
118 r  typeof 

4
找出数字是否为Java中的Double
我是Java新手。我试图找出一个数字是否为Double,如下所示: if ( typeof ( items.elementAt(1) )== Double ) { sum.add( i, items.elementAt(1)); } 如果有人能告诉我如何重新排列语法以使其正常工作,将不胜感激。
92 java  typeof 

16
测试值是否为函数
我需要测试表单的值是否onsubmit为函数。格式通常为onsubmit="return valid();"。有没有办法判断这是否是一个函数,以及它是否可调用?使用typeof只是返回它是一个字符串,对我没有多大帮助。 编辑:当然,我知道“返回有效的();” 是一个字符串。我将replace其简化为“ valid();”,甚至是“ valid()”。我想知道这两个是否是一个函数。 编辑:这是一些代码,可能有助于解释我的问题: $("a.button").parents("form").submit(function() { var submit_function = $("a.button").parents("form").attr("onsubmit"); if ( submit_function && typeof( submit_function.replace(/return /,"") ) == 'function' ) { return eval(submit_function.replace(/return /,"")); } else { alert("onSubmit is not a function.\n\nIs the script included?"); return false; } } ); 编辑2:这是新的代码。似乎我仍然必须使用eval,因为调用form.submit()不会触发现有的onsubmit。 var formObj = $("a.button").parents("form"); formObj.submit(function() …

5
JavaScript:检测参数是否为数组而不是对象(Node.JS)
我应该如何检测参数是否为数组,因为typeof []return,'object'并且我想区分数组和对象。 该对象可能看起来像,{"0":"string","1":"string","length":"2"}但如果它实际上是一个看起来像数组的对象,我不希望它作为数组出现。 JSON.parse并JSON.stringify能够做出这种区分。我该怎么做? 我正在使用与Chrome相同的基于V8的Node.JS。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.