几乎解决
几乎成功让它工作了。
但是,这个解决方案有缺陷,因为主机对象有时与本地对象无法区分。当在Chrome上测试isNative(window.alert)
时,以下代码会失败,因为webkit引擎定义了一个看起来与原生alert函数完全相同的alert
函数。
它使用普通的ES3 JavaScript,并基于测试对象是否为本地(而不是Host对象)的方法。然而,根据ES3 Host对象的定义:"任何不是本地的对象都是host对象",这个函数可以用来检测host对象。
function isNative(obj) {
switch(typeof obj) {
case 'number': case 'string': case 'boolean':
return false;
}
if (!(obj instanceof Object)) return false;
if (typeof obj.constructor === 'undefined') return false;
if (!(obj.constructor instanceof Function)) return false;
return true;
}
if (typeof myObject === 'object' || typeof myObject === 'function')
alert(isNative(myObject) ? 'Native Object' : 'Host Object');
这里有一个JsFiddle测试列表,可以用于在IE / Firefox / Chrome中测试此内容。
由于代码非常基础,我没有测试非浏览器环境,但我认为不会有任何问题。
var n = 0;
function assert(condition, message) {
n++;
if (condition !== true) {
document.write(n + '. FAILS: ' + (message || '(no message)') + '<br/>');
} else {
document.write(n + '. PASS: ' + (message || '(no message)') + '<br/>');
}
}
assert(isNative({}), '{} -- Plain object');
assert(isNative(function() {}), 'function() {} -- Plain function');
assert(isNative([]), '[] -- Plain array');
assert(isNative(/regex/), '/regex/ - Native regex');
assert(isNative(new Date()), 'new Date() - Native date object through instantiation');
assert(isNative(new String('string')), 'new String("string") - Native string object through instantiation');
assert(isNative(new Number(1)), 'new Number(1) - Native number object through instantiation');
assert(isNative(new Boolean(true)), 'new Boolean(true) - Native boolean object through instantiation');
assert(isNative(new Array()), 'new Array() - Native array object through instantiation');
assert(isNative(new Object()), '{} -- new Object() - Native object through instantiation');
assert(isNative(new Function('alert(1)')), '{} -- Native function through instantiation');
var Animal = function() {};
var animal = new Animal();
var Dog = function() {};
Dog.prototype = animal;
var dog = new Dog();
assert(isNative(Animal), 'Animal -- User defined type');
assert(isNative(animal), 'animal -- Instance of User defined type');
assert(isNative(Dog), 'Dog -- User defined inherited type');
assert(isNative(dog), 'dog -- Instance of User defined inherited type');
assert(isNative(Object), 'Object -- Built in');
assert(isNative(Array), 'Array -- Built in');
assert(isNative(Date), 'Date -- Built in');
assert(isNative(Boolean), 'Boolean -- Built in');
assert(isNative(String), 'String -- Built in');
assert(isNative(Function), 'Function -- Built in');
assert(!isNative('string'), '"string" - Primitive string');
assert(!isNative(1), '1 - Primitive number');
assert(!isNative(true), 'true - Primitive boolean');
assert(!isNative(null), 'null - Primitive null');
assert(!isNative(NaN), 'NaN - Primitive number NotANumber');
assert(!isNative(Infinity), 'Infinity - Primitive number Infinity');
assert(!isNative(undefined), 'undefined - Primitive value undefined');
assert(!isNative(window), 'window -- Host object');
assert(!isNative(alert), 'alert -- Host function');
assert(!isNative(document), 'document -- Host object');
assert(!isNative(location), 'location -- Host object');
assert(!isNative(navigator), 'navigator -- Host object');
assert(!isNative(parent), 'parent -- Host object');
assert(!isNative(frames), 'frames -- Host object');
Object.prototype.toString.call(obj)
并查看是否返回[object Object]
(这是纯对象/实例生成的内容)或类似[object HTMLBodyElement]
的信息以确定它是否为主机对象。 - pimvdb