现在的位置: 首页 > web前端 > 正文

javascript 对象类型检查

2019年11月17日 web前端 ⁄ 共 2648字 ⁄ 字号 评论关闭

变量的类型检查表

变量                   typeof变量                 变量、构造函数(注意大写
)

{an:"object"}       object                        O
bject

["an","array"]      object                        A
rray

function(){}         function                      F
unction

"a string"           string                         S
tring

55                     number                      N
umber

true                   boolean                     B
oolean

new User()         object                        User

 

 

/*2-7使用typeof
来判断一个对象类型的例子*/
/*parseInt
方法返回与保存在 numString 中的数字值相等的整数
parseInt("abc");//返回NaN       ,用isNaN方法检测NaN
parseInt("12abc");//返回12
*/
//检查我的的数字是否是字符串
var num="12abc";
//var arr=new Array("aa","bb","cc");
var arr="aa,aa,a,,";
var str="mmmm";
//alert(arr);
if(typeof num=="string"
)
{
    //若是根据这个字符串解析出整数来
    num=parseInt(num);
    alert(num);//这里12
}
//    //检查数组是否实际上是字符串
if(typeof arr=="string")
{
    //若是,则根据逗号切分出数组来
    //arr=arr.split(",")
    alert(arr);

else{
   // arr=arr.split(",")
    alert(arr);
}

/*2-8使用构造函数属性constructor
来判断对象的类型*/
检查我的的数字是否是字符串
if(num.constructor==String)

    num=parseInt(num);
    alert(num)
//检查数组是否实际上是字符串
if(str.constructor==Array)
    str=str.join(',');
    alert(str);

/*api JavaScript constructor 属性:constructor 属性返回对创建此对象的 Boolean 函数的引用。语法:object.constructor
*/
var test=new Boolean();

if (test.constructor==Array)
{
document.write("This is an Array");
}
if (test.constructor==Boolean)
{
document.write("This is a Boolean");
}
if (test.constructor==Date)
{
document.write("This is a Date");
}
if (test.constructor==String)
{
document.write("This is a String");
}
输出:This is a Boolean

/*2-9一个函数,可以用来严格维护传入函数的所有参数*/
function strict(types,args){
    //保证类型的数量和参数的数量相匹配
    if(types.length!=args.length){
        throw "Invalid number of arguments.Expected"+ types.length+ ",received" +args.length +"      instead";
    }
   
    //遍历所有的参数,检查它们的类型
    for (var i=0;i<args.length;i++)
    {
        if(args[i].constructor!=types[i])
        {
            throw "Invalid argument type. Expected "+ types[i].name+",received "+args[i].constructor.name+" instead";
        }//如果把userList中的strict([String,Number,Array],arguments);换成strict([String,Number,Object],arguments);//则在firefox中显示uncaught exception: Invalid argument type. Expected Object,received Array instead
    }
   
  
   
  
}

 
//一个简单的函数,打印用户列表

function userList(prefix,num,users){
    //保证需要prefix是字符串,num是数字,users是数组
    strict([String,Number,Array],arguments);
   
    //遍历'num'个用户
    for(var i=0;i<num;i++)
    {
        //显示第个用户的信息
        alert(prefix+":"+users[i]);
    }
}

 function test()
    {
        var prefix="ddd";
        var num=parseInt("3");
        var user=["11","22",""];//新建数组,并直按赋值,显示dd:11然后显示ddd:22,最后显示ddd:
//        var user=new Array();//新建数组, 显示ddd:11然后显示ddd:22,最后显示ddd:undefined
//        user[0]="11";//给数组赋值
//        user[1]="22";
        userList(prefix,num,user);//
       
    }

抱歉!评论已关闭.