var a = {},
b = function(){},
c = [];
console.log(Object.prototype.toString.call(a));  //"[object Object]"
console.log(Object.prototype.toString.call(b));  //"[object Function]"
console.log(Object.prototype.toString.call(c));  //"[object Array]"看看,其实这个是为了能查看,对象的类型的总的来说,就是js中,所有类型都是基于Object的(除了ie8-版本中的dom元素),Function,Array等引用类型,都是继承自Object对象的。这种获得对象类型的方法,也算是当前比较常用的了,jquery就是用的这个方法。至于这个字符串的组成,第一个object,我认为是说,这个元素是继承自Object的,第二个Object就是说,它现在属于引用类型中,具体的哪一种,我代码中的方式返回的字符串,并且前后需要有中括号的。

解决方案 »

  1.   

    alert,这函数,咋处理,
    对Primitive Types直接输出值。
    对非Primitive Types,输出其toString 方法的到的字符串。toString方法到底怎么处理。
    对function Object(){}      Object.prototype.toString  我猜想是这样的。
    Object.prototype.toString = function(){
    var s = '[object';
    var test = [Function,Boolean,Number,String,Array,Date,Object];
    var testMap = ['Function','Boolean','Number','String','Array','Date','Object'];
    //s +=typeof this;
    for(var i=0;i<test.length;i++){
    if(this instanceof test[i]){
    s +=' ' + testMap[i] + ']';
    break;
    }
    }
    return s;
    }