如何检查参数的类型? [英] How to check typeof arguments?

查看:80
本文介绍了如何检查参数的类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个通用功能(JavaScript新手在这里,所以不要以为我会给你留下深刻的印象

你):


功能blah( )

{

var container ='''';

for(var i = 0; i< arguments.length; i ++)

{

容器+ =参数[i] +''\ n'';

}

警告(容器);

}


现在,如果我这样称呼:


blah(''我,''我自己',''我');


我得到:


警告(你知道,警报对话框)



我自己




然而,如果我用数组:


var arr_obj = new Array(''Me'',''我自己'',''我');

blah(arr_obj );


我得到:


ALERT

我,我自己,我

我试过检查参数对象的类型,但无论传递什么,参数

alwa ys有一种''对象''。


我有什么遗漏,如何检查我是否通过了数组或

a正常的逗号分隔值?


另外,如果我在这里遗漏了一些基本的东西,请原谅我。谢谢!


-Lost

I have this generic function (JavaScript newbie here, so don''t think I am going to impress
you):

function blah()
{
var container = '''';
for(var i = 0; i < arguments.length; i++)
{
container += arguments[i] + ''\n'';
}
alert(container);
}

Now, if I call this like:

blah(''Me'', ''Myself'', ''I'');

I get:

ALERT (you know, the alert dialog)
Me
Myself
I

However, if I call it with an array:

var arr_obj = new Array(''Me'', ''Myself'', ''I'');
blah(arr_obj);

I get:

ALERT
Me,Myself,I

I tried checking the type of the arguments object but no matter what is passed, arguments
always has a type of ''object''.

Is there something I am missing as to how to check whether or not I passed it an array or
a normal comma-separated string of values?

Also, forgive me if I am missing some fundamental thing here. Thanks!

-Lost

推荐答案

我试过检查参数对象的类型
I tried checking the type of the arguments object

但无论传递什么,参数总是有一种''对象''。
but no matter what is passed, arguments always has a type of ''object''.



参数对象是 - 但不是传递给函数的参数。


函数f(){

alert(typeof arguments [0]);

}


f(''foobar''); //提醒''字符串''

arguments object is - but not arguments passed to function.

function f() {
alert(typeof arguments[0]);
}

f(''foobar''); // alerts ''string''


有什么我错过了
Is there something I am missing



当你做

var something + = arguments [0] +''\ n'';

然后你为参数[0]调用toString方法。如果您已将

数组引用作为参数传递,则调用toString for array对象。并且数组toString的
返回逗号分隔的值字符串。

when you do
var something+= arguments[0] + ''\n'';
then you call toString method for arguments[0]. If you have passed an
array reference as argument, toString for array object is called. And
for arrays toString returns comma-separated string of values.


关于如何检查我是否将数组传递给它

a正常逗号分隔的值字符串?
as to how to check whether or not I passed it an array or
a normal comma-separated string of values?



if(typeof arguments [i] ==''string''){

//字符串参数

}

else if(arguments [i] instanceof Array){

//数组参数

}

请注意,在两种情况下,使用/或
中的instanceof都不能统一支票。 JavaScript中的隐式和显式字符串构造函数

是两个独立的实体,所以说

(arguments [i] instanceof String)仅对显式字符串为真

通过新字符串创建的对象(某事物) - 你非常希望b / b
很少想做的事情。

if (typeof arguments[i] == ''string'') {
// string argument
}
else if (arguments[i] instanceof Array) {
// array argument
}

Please not that you cannot uniform the check by using instanceof in
both cases. Implicit and explicit string constructors in JavaScript
are two separate entities, so say
(arguments[i] instanceof String) will be true only for explicit string
objects created over new String("something") - something you very
rarely want to do.


" -Lost" < sp **************** @ REMOVEMEcomcast.net写在

新闻:wo *************** ***************@comcast.com:
"-Lost" <sp****************@REMOVEMEcomcast.netwrote in
news:wo******************************@comcast.com:

我有这个通用功能(JavaScript新手在这里,所以不要''我想我会给你留下深刻的印象:


功能等等()

{

var container ='''';

for(var i = 0; i< arguments.length; i ++)

{

container + = arguments [i] +''\ n'';

}

alert(容器);

}


现在,如果我这样称呼:


blah(''我'',''我自己',''我'');


我得到:


ALERT(你知道,警报对话框)



我自己




但是,如果我用数组调用它:


var arr_obj =新阵列(''我',''我自己',''我');

blah(arr_obj);


我得到:


AL ERT

我,我自己,我


我试过检查参数对象的类型,但不管是什么

被传递,参数总是有一种''对象''。


我是否缺少关于如何检查我是否b / b $ b通过它的问题数组或正常的逗号分隔值?


另外,如果我在这里遗漏了一些基本的东西,请原谅我。谢谢!
I have this generic function (JavaScript newbie here, so don''t think I
am going to impress you):

function blah()
{
var container = '''';
for(var i = 0; i < arguments.length; i++)
{
container += arguments[i] + ''\n'';
}
alert(container);
}

Now, if I call this like:

blah(''Me'', ''Myself'', ''I'');

I get:

ALERT (you know, the alert dialog)
Me
Myself
I

However, if I call it with an array:

var arr_obj = new Array(''Me'', ''Myself'', ''I'');
blah(arr_obj);

I get:

ALERT
Me,Myself,I

I tried checking the type of the arguments object but no matter what
is passed, arguments always has a type of ''object''.

Is there something I am missing as to how to check whether or not I
passed it an array or a normal comma-separated string of values?

Also, forgive me if I am missing some fundamental thing here. Thanks!



请参阅本教程了解typeof和dltypeof:
http://www.webreference.com/dhtml/column68/


-Lost写道:
-Lost wrote:

我有这个通用功能(JavaScript新手在这里,所以不要以为我会留下深刻的印象

你):
I have this generic function (JavaScript newbie here, so don''t think I am going to impress
you):



[...]

[...]


我试过检查参数对象的类型,但无论传递什么,参数

总是有一种''对象''。


我是否缺少关于如何检查是否通过数组或

a正常逗号分隔的值字符串?
I tried checking the type of the arguments object but no matter what is passed, arguments
always has a type of ''object''.

Is there something I am missing as to how to check whether or not I passed it an array or
a normal comma-separated string of values?



类型操作符的工作方式在

ECMAScript语言规范的第11.4.3节中规定。根据提供的表达式

,它返回以下之一:


类型结果

未定义未定义

Null" object"
Boolean" boolean"

Number" number"

String" string"

对象(本机不是

实现[[Call]])" object"

Object(native和

实现[[Call]])" function"

对象(主机)依赖于实现

唯一棘手的部分是对象被拆分为普通对象和

函数对象。


javascript数组是使用内置数组

函数对象构造的对象作为构造函数,因此类型为数组返回

''函数''。函数的参数对象不是函数,因此类型

参数返回''对象''。


如果你想了解更多关于一个对象,你可以尝试构造函数

属性,但这可能在浏览器中不一致。使用instanceOf可能更好




var x = [];

alert(typeof x)//对象

alert(x instanceof Array); // true

要确定传递给函数的参数是否是字符串,数字

或数组,请执行以下操作:


function foo(){

var arg,args = arguments;

var len = args.length;


for(var i = 0; i< len; i ++){

arg = args [i];


if(typeof arg ==''string''| | typeof arg ==''number''){

alert(''arg''+ i +''是一个字符串或数字'');

} else if(arg instanceof Array){

alert(''arg''+ i +''是一个数组'');

} else if(arg instanceof Function) ){

alert(''arg''+ i +''是一个函数'');

}

}

}


foo([],6,''blah'',function(){});

-

Rob

How the typeof operator works is specified in section 11.4.3 of the
ECMAScript Language specification. Depending on the expression
provided, it returns one of:

Type Result
Undefined "undefined"
Null "object"
Boolean "boolean"
Number "number"
String "string"
Object (native doesn?t
implement [[Call]]) "object"
Object (native and
implements [[Call]]) "function"
Object (host) Implementation-dependent
The only tricky part is that objects are split into plain objects and
function objects.

A javascript Array is an object constructed using the built-in Array
function object as a constructor, therefore typeof Array returns
''function''. A function''s arguments object isn''t a function, so typeof
arguments returns ''object''.

If you want to know more about an object, you can try the constructor
property but that can be inconsistent across browsers. It may be better
to use instanceOf.

var x = [];
alert(typeof x) // object
alert(x instanceof Array); // true
To determine whether arguments passed to a function are strings, numbers
or arrays, do something like:

function foo () {
var arg, args = arguments;
var len = args.length;

for (var i=0; i<len; i++){
arg = args[i];

if (typeof arg == ''string'' || typeof arg == ''number''){
alert(''arg '' + i + '' is a string or number'');
} else if (arg instanceof Array){
alert(''arg '' + i + '' is an Array'');
} else if (arg instanceof Function){
alert(''arg '' + i + '' is a Function'');
}
}
}

foo( [], 6, ''blah'', function(){} );
--
Rob


这篇关于如何检查参数的类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆