__proto__ 属性的 Polyfill 及其限制
该 polyfill 为不支持 __proto__
属性的浏览器提供兼容性解决方案。它通过 Object.defineProperty
等方法模拟 __proto__
的行为,但仍存在一些限制。
例如,以下代码在某些旧浏览器中可能无法正常工作:
// ES5 版本
var X = function () {};
X.prototype = {
foo: function () {
return 'xFoo by instance!';
}
};
X.foo = function () {
return 'xFoo';
};
X.s = { s: 'x' };
X.f = 'X';
var Y = function () {};
Y.prototype = Object.create(X.prototype);
Y.prototype.constructor = Y;
Y.foo = function () {
return 'yFoo';
};
// ES6 版本
class X {
static get foo() {
return 'xFoo';
}
get foo() {
return this.constructor.foo + ' by instance!';
}
}
X.s = { s: 'x' };
X.f = 'X';
class Y extends X {
static get foo() {
return 'yFoo';
}
}
不支持该 polyfill 的浏览器包括 IE8、IE9 和 IE10。
proto-polyfill-master.zip
预估大小:12个文件
proto-polyfill-master
文件夹
index.js
6KB
package.json
1KB
tests
文件夹
core.js
5KB
limitations.js
2KB
index.html
832B
core-plus.js
2KB
class-like.js
4KB
LICENSE
1KB
8.77KB
文件大小:
评论区