想要与或同时使用 and 运算符

want to use and operator with or at the same time

var obj5 = {a:200, b:200, c:true};
var dataT = 'string' || 'number' || 'boolean' ;
obj5.notType = function(){
  for (var p in this){
    if(typeof(this[p]) == dataT){
      alert("have datatype");}
    }
};
obj5.notType();  // doesn't show alert 

这里我需要检查所有对象 属性 数据是否为有效数据类型,但我无法做到。谁能帮帮我吗?

if(typeof(this[p]) == 'string' 
|| typeof(this[p]) == 'number' 
|| typeof(this[p]) == 'boolean')
{
 alert("have datatype");
}

您不能根据表达式的计算结果将表达式分配给变量。

'string' || 'number' || 'boolean' ; 的计算结果为 'string' 因此您始终要测试该值是否为字符串。

改为将您的数据类型名称存储在数组中。然后用indexOf看是否匹配

var dataT = ['string', 'number', 'boolean'] ;

if ( dataT.indexOf(typeof this[p]) > -1 ) {