相关疑难解决方法(0)

为什么4不是Number的实例?

只是好奇:

  • 4 instanceof Number => false
  • new Number(4)instanceof Number => true?

为什么是这样?与字符串相同:

  • 'some string' instanceof String 返回false
  • new String('some string') instanceof String =>是的
  • String('some string') instanceof String 也返回false
  • ('some string').toString instanceof String 也返回false

对于对象,数组或函数类型,instanceof运算符按预期工作.我只是不知道如何理解这一点.

[ 新见解 ]

Object.prototype.is = function() {
        var test = arguments.length ? [].slice.call(arguments) : null
           ,self = this.constructor;
        return test ? !!(test.filter(function(a){return a === self}).length)
               : (this.constructor.name ||
                  (String(self).match ( /^function\s*([^\s(]+)/im )
                    || [0,'ANONYMOUS_CONSTRUCTOR']) [1] );
}
// usage
var Newclass = function(){};  // anonymous Constructor …
Run Code Online (Sandbox Code Playgroud)

javascript

64
推荐指数
2
解决办法
3万
查看次数

标签 统计

javascript ×1