我现在有这样一个需求:
function Run1(zhangwei, haha) {
        strict([String, Number], arguments);
    }
function strict(types, args) {
        try {
            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.";
                }
            }
        }
        catch (er) {
            alert(er);
        }
    }在以上代码中,标红的地方,我现在无法获取到“String”或者是“Number”这两个字符串。因为类型String和Number都是类型,当我们获取constructor时,他会返回一个类型的构造函数,我现在就是想获取到这个构造函数的名字,从而显示出类型名。请高手帮忙急急急

解决方案 »

  1.   

    函数名只是一个内存地址的引用,一个变量名而已,和类型没有关系的
    声明一个函数,用它构造一个对象,然后给函数重新赋值,再构造一个对象,这两个对象构造函数名都是一个,但是一个类型么??所以说函数的名称与类型是没有关系的
    js里面所有对象都是Object类型,只是扩展的成员不同罢了
      

  2.   

    用个正则提取throw "Invalid argument type. Expected " + types[i].toString().match(/function (\w+)/)[1] + ", received " + args[i].constructor.toString().match(/function (\w+)/)[1] + " instead.";