IE8 getPrototypeOf method

浪尽此生 提交于 2019-11-27 16:35:37

问题


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



来源:https://stackoverflow.com/questions/10919915/ie8-getprototypeof-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!