Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/450.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 当x可能是几种类型时,如何知道x是什么_Javascript - Fatal编程技术网

Javascript 当x可能是几种类型时,如何知道x是什么

Javascript 当x可能是几种类型时,如何知道x是什么,javascript,Javascript,如何知道js中的x是什么 有时我不得不编写一个带有参数的函数,参数有多种类型。如试验(x);在运行代码之前,我需要知道x是什么 <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script> <div class="hello">hello</div> <script type="tex

如何知道js中的x是什么

有时我不得不编写一个带有参数的函数,参数有多种类型。如试验(x);在运行代码之前,我需要知道x是什么

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<div class="hello">hello</div>
<script type="text/javascript">

    // the x var will be random 1 of 4 bellow , how to know what is it ?
    var x = $('.hello')[0];
    var x = ['1','2'];
    var x = {'name':'jimmy'};
    var x = 'hello';

    if(???){
        // alert x is a html node
    }
    if(???){
        // alert x is a js arr
    }
    if(???){
        // alert x is a js obj
    }
    if(???){
        // alert x is a js str
}

</script>

你好
//x var将是以下4个变量中的随机1个,如何知道它是什么?
var x=$('.hello')[0];
变量x=['1','2'];
var x={'name':'jimmy'};
var x='hello';
如果(??){
//alert x是一个html节点
}
如果(??){
//警报x是js arr
}
如果(??){
//警报x是一个js obj
}
如果(??){
//警报x是一个js str
}


在这种情况下,这将起作用:

var x = $('.hello')[0];
var x = ['1','2'];
var x = {'name':'jimmy'};
var x = 'hello';

if(x === undefined){
    // alert x is undefined
}else if(x === null){
    // alert x is null
}else if('nodeType' in x){
    // alert x is a html node
}else if(x instanceof Array){
    // alert x is a js arr
}else if(typeof x == 'string'){
    // alert x is a js str
}else if(x.constructor == Object){
    // alert x is a js obj
}
最常用的方法是通过每个对象具有的唯一属性来检测类型(
if(x.push)
),因为js是一种“duck-type”语言。但是对于这种情况,上面的代码可以正常工作


希望这有帮助。干杯

在这种情况下,这将起作用:

var x = $('.hello')[0];
var x = ['1','2'];
var x = {'name':'jimmy'};
var x = 'hello';

if(x === undefined){
    // alert x is undefined
}else if(x === null){
    // alert x is null
}else if('nodeType' in x){
    // alert x is a html node
}else if(x instanceof Array){
    // alert x is a js arr
}else if(typeof x == 'string'){
    // alert x is a js str
}else if(x.constructor == Object){
    // alert x is a js obj
}
最常用的方法是通过每个对象具有的唯一属性来检测类型(
if(x.push)
),因为js是一种“duck-type”语言。但是对于这种情况,上面的代码可以正常工作


希望这有帮助。干杯

在这种情况下,这将起作用:

var x = $('.hello')[0];
var x = ['1','2'];
var x = {'name':'jimmy'};
var x = 'hello';

if(x === undefined){
    // alert x is undefined
}else if(x === null){
    // alert x is null
}else if('nodeType' in x){
    // alert x is a html node
}else if(x instanceof Array){
    // alert x is a js arr
}else if(typeof x == 'string'){
    // alert x is a js str
}else if(x.constructor == Object){
    // alert x is a js obj
}
最常用的方法是通过每个对象具有的唯一属性来检测类型(
if(x.push)
),因为js是一种“duck-type”语言。但是对于这种情况,上面的代码可以正常工作


希望这有帮助。干杯

在这种情况下,这将起作用:

var x = $('.hello')[0];
var x = ['1','2'];
var x = {'name':'jimmy'};
var x = 'hello';

if(x === undefined){
    // alert x is undefined
}else if(x === null){
    // alert x is null
}else if('nodeType' in x){
    // alert x is a html node
}else if(x instanceof Array){
    // alert x is a js arr
}else if(typeof x == 'string'){
    // alert x is a js str
}else if(x.constructor == Object){
    // alert x is a js obj
}
最常用的方法是通过每个对象具有的唯一属性来检测类型(
if(x.push)
),因为js是一种“duck-type”语言。但是对于这种情况,上面的代码可以正常工作


希望这有帮助。干杯

使用类型。。。如果(类型(x))='S.T')执行stuff@Popnoodles不幸的是,这只是部分原因。例如,
x
的1、2和4都是jQuery的
实例的类型。你肯定能解决剩下的问题?@kiyarash typeof是一个运算符,不是一个函数,所以你不需要括号来使用它。typeof use type of。。。如果(类型(x))='S.T')执行stuff@Popnoodles不幸的是,这只是部分原因。例如,
x
的1、2和4都是jQuery的
实例的类型。你肯定能解决剩下的问题?@kiyarash typeof是一个运算符,不是一个函数,所以你不需要括号来使用它。typeof use type of。。。如果(类型(x))='S.T')执行stuff@Popnoodles不幸的是,这只是部分原因。例如,
x
的1、2和4都是jQuery的
实例的类型。你肯定能解决剩下的问题?@kiyarash typeof是一个运算符,不是一个函数,所以你不需要括号来使用它。typeof use type of。。。如果(类型(x))='S.T')执行stuff@Popnoodles不幸的是,这只是部分原因。例如,
x
的1、2和4都是jQuery的
实例的类型。你肯定能解决剩下的问题?@kiyarash-typeof是一个操作符,不是一个函数,所以你不需要括号来使用它。