__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。

zip
proto-polyfill-master.zip 预估大小:12个文件
folder
proto-polyfill-master 文件夹
file
index.js 6KB
file
package.json 1KB
folder
tests 文件夹
file
core.js 5KB
file
limitations.js 2KB
file
index.html 832B
file
core-plus.js 2KB
file
class-like.js 4KB
file
LICENSE 1KB
file
package-lock.json 77B
file
.npmignore 11B
file
CHANGELOG.md 130B
file
README.md 3KB
zip 文件大小:8.77KB