IE8 getPrototypeOf method

2019-01-23 20:08发布

问题:

Pretty simple:

I have code using Object.getPrototypeOf(...) to get the inherited classes of a Dojo Widget (just a JS object). Object.getPrototypeOf(...) isn't supported in IE8. I need an IE work around. Any ideas? Thanks in advance.

回答1:

Jon Resig's polyfill works http://ejohn.org/blog/objectgetprototypeof/

I have made it even smaller

if (typeof Object.getPrototypeOf !== "function")
    Object.getPrototypeOf = "".__proto__ === String.prototype
        ? function (object) {
            return object.__proto__;
        }
        : function (object) {
            // May break if the constructor has been tampered with
            return object.constructor.prototype;
        };


回答2:

Use https://github.com/kriskowal/es5-shim. Among other things, it supports Object.getPrototypeOf.

Source: ECMAScript 5 polyfills from Modernizr project



回答3:

Classes created with Dojo.declared store metadata with their superclasses so you don't need to use getPrototypeOf.

I think you can get the first superclass with

MyClass.prototype.constructor._meta.bases[1]

and its prototype with

MyClass.prototype.constructor._meta.bases[1].prototype

(bases[0] seems to be the class itself)


Although why are you even needing to get the prototype? Its very likely you will end up reimplementing some feature that is already provided by dojo.declare