在原型被密封时寻找用于填充物体的技巧 [英] Looking for hacks to shim objects when their prototype are sealed

查看:98
本文介绍了在原型被密封时寻找用于填充物体的技巧的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的上下文中,String和String.prototype,Array和Array.prototype,Object和Object.prototype是密封的.我无法控制这种密封. 我的上下文仅支持ES5,我想使用ES6填充程序( https://github.com/paulmillr/es6-shim )用于ES6的全功能方法,例如find(),reduce()甚至startsWith()...

In my context, String and String.prototype, Array and Array.prototype, Object and Object.prototype are sealed. I have no control on this sealing. My context only supports ES5 and I would like to use an ES6 shim (https://github.com/paulmillr/es6-shim) for use ES6 gracefull methods like find(), reduce() or even startsWith() ...

我不能使用这些补片,因为当我尝试添加或编辑原型时会抛出错误.您知道一些技巧来绕过此限制吗?我的意思是在不提供我自己的实现的情况下使用Array.prototype.find().

I can't use theses shims because error are throw when I try to add or edit prototypes. Do you know some tricks to bypass this restriction. I mean using Array.prototype.find() without providing my own implementation.

例如,对于一个在我可以使用ES6函数之前可以使用新的ES6Array([])的解决方案,对我来说将是一个合适的解决方案.我只是想避免自己执行Shims.我想尽可能多地使用paulmillr实现.

For example, a solution where I could use new ES6Array([]), before being able to use ES6 function, will be a suitable solution for me. I just want to avoid making my own implementation of Shims. I want to use the paulmillr implementation as much as possible.

是否可以使用影子黑客来使用paulmillr实现? 我一直在努力做到这一点,但这并不会导致任何合规的结果.

Is it possible to use shadowing hacks to use paulmillr implementations ? I've been trying on my side to do that but it doesn't lead to any compliant result.

PS:这是Salesforce Commerce Cloud上下文

PS : This is a Salesforce Commerce Cloud context

推荐答案

es6-shim是一个polyfill,如果原型被密封,肯定会失败,因为它应该修改它们.

es6-shim is a polyfill and will surely fail if prototypes are sealed, because it's supposed to modify them.

此处需要的方法是 ponyfill .如描述所述,

The approach that is needed here is ponyfill. As the description says,

通常,您不应该修改您不拥有的API.

In general, you should not modify API's you don't own.

相反,ponyfill不会猴子修补任何东西,而是将功能导出为普通模块,因此您可以在本地使用它而不会影响其他代码.

A ponyfill, in contrast, doesn't monkey patch anything, but instead exports the functionality as a normal module, so you can use it locally without affecting other code.

tl; dr; Polyfills修补本地API时很顽皮,而ponyfills是纯净的,不会影响环境.

tl;dr; Polyfills are naughty as they patch native APIs, while ponyfills are pure and don't affect the environment.

其中一些在NPM上用 ponyfill关键字标记.

Some of them are labeled on NPM with ponyfill keyword.

core-js提供了不会污染全局名称空间的库版本因此可以用作小马宝,所以通常没有理由进一步寻找:

core-js provides library version of it that doesn't pollute global namespace and thus can be used as a ponyfill, so generally there's no reason to look any further:

import * as find from 'core-js/library/fn/array/find';

find(array, 'foo')

请注意,某些core-js/library导出是polyfilled实现(PromiseMap等):

Notice that some core-js/library exports are polyfilled implementations (Promise, Map, etc.):

import { Promise as PolyfilledPromise } from 'core-js/library';

PolyfilledPromise.resolve();

其他方法是多填充方法的集合,因为为它们提供独立的实现是不切实际或不可能的(ArrayObject):

While other ones are collections of polyfilled methods, because it's impractical or impossible to provide independent implementation for them (Array, Object):

import { Array as arrayPonyfills } from 'core-js/library';
const { find } = arrayPonyfills;

find.call(array, 'foo');

这篇关于在原型被密封时寻找用于填充物体的技巧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆