几乎解决了
几乎设法让这个工作。
该解决方案的不足之处在于 Host 对象有时与 Native 对象无法区分。下面的代码在 Chrome 上测试时失败,isNative(window.alert)
因为 webkit 引擎定义了一个alert
(到目前为止)看起来与本机相同的函数。
它根据 ES3 使用纯 javascript,并且基于测试对象是本机的(而不是 Host 对象)。但是,根据 ES3 对主机对象的定义:“任何非本地对象都是主机对象。” 此功能可用于检测主机对象。
// ISNATIVE OBJECT DETECTION
function isNative(obj) {
switch(typeof obj) {
case 'number': case 'string': case 'boolean':
// Primitive types are not native objects
return false;
}
// Should be an instance of an Object
if (!(obj instanceof Object)) return false;
// Should have a constructor that is an instance of Function
if (typeof obj.constructor === 'undefined') return false;
if (!(obj.constructor instanceof Function)) return false;
return true;
}
// CHECK IF AN OBJECT IS HOST OR NATIVE
if (typeof myObject === 'object' || typeof myObject === 'function')
alert(isNative(myObject) ? 'Native Object' : 'Host Object');
这是可用于在 IE / Firefox / Chrome 中进行测试的 JsFiddle 测试列表。
我还没有测试过非浏览器环境,因为它有点麻烦,但由于代码非常基本,我认为它不会有任何问题。
// ASSERT HELPER FUNCTION
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/>');
}
}
// USER CREATED OBJECTS
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');
// USER OBJECT INSTANTIATION AND INHERITANCE
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');
// BUILT IN OBJECTS
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');
// PRIMITIVE TYPES
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');
// HOST OBJECTS
assert(!isNative(window), 'window -- Host object');
assert(!isNative(alert), 'alert -- Host function'); // fails on chrome
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');