懒羊羊
2023-11-14 8286c62256f23bc2367a6729c0f46f84215e380b
提交 | 用户 | 时间
8286c6 1
2 `es5-shim.js` and `es5-shim.min.js` monkey-patch a JavaScript context to
3 contain all EcmaScript 5 methods that can be faithfully emulated with a
4 legacy JavaScript engine.
5
6 `es5-sham.js` and `es5-sham.min.js` monkey-patch other ES5 methods as
7 closely as possible.  For these methods, as closely as possible to ES5
8 is not very close.  Many of these shams are intended only to allow code
9 to be written to ES5 without causing run-time errors in older engines.
10 In many cases, this means that these shams cause many ES5 methods to
11 silently fail.  Decide carefully whether this is what you want.
12
13
14 ## Tests
15
16 The tests are written with the Jasmine BDD test framework.
17 To run the tests, navigate to <root-folder>/tests/. 
18
19 In order to run against the shim-code, the tests attempt to kill the current 
20 implementation of the missing methods. This happens in <root-folder>/tests/helpers/h-kill.js.
21 So in order to run the tests against the built-in methods, invalidate that file somehow
22 (comment-out, delete the file, delete the script-tag, etc.).
23
24 ## Shims
25
26 ### Complete tests ###
27
28 * Array.prototype.every
29 * Array.prototype.filter
30 * Array.prototype.forEach
31 * Array.prototype.indexOf
32 * Array.prototype.lastIndexOf
33 * Array.prototype.map
34 * Array.prototype.some
35 * Array.prototype.reduce
36 * Array.prototype.reduceRight
37 * Array.isArray
38 * Date.now
39 * Date.prototype.toJSON
40 * Function.prototype.bind
41     * /!\ Caveat: the bound function's length is always 0.
42     * /!\ Caveat: the bound function has a prototype property.
43     * /!\ Caveat: bound functions do not try too hard to keep you
44       from manipulating their ``arguments`` and ``caller`` properties.
45     * /!\ Caveat: bound functions don't have checks in ``call`` and
46       ``apply`` to avoid executing as a constructor.
47 * Object.keys
48 * String.prototype.trim
49
50 ### Untested ###
51
52 * Date.parse (for ISO parsing)
53 * Date.prototype.toISOString
54
55 ## Shams
56
57 * /?\ Object.create
58
59     For the case of simply "begetting" an object that
60     inherits prototypically from another, this should work
61     fine across legacy engines.
62
63     /!\ Object.create(null) will work only in browsers that
64     support prototype assignment.  This creates an object
65     that does not have any properties inherited from
66     Object.prototype.  It will silently fail otherwise.
67
68     /!\ The second argument is passed to
69     Object.defineProperties which will probably fail
70     silently.
71
72 * /?\ Object.getPrototypeOf
73
74     This will return "undefined" in some cases.  It uses
75     __proto__ if it's available.  Failing that, it uses
76     constructor.prototype, which depends on the constructor
77     property of the object's prototype having not been
78     replaced.  If your object was created like this, it
79     won't work:
80
81         function Foo() {
82         }
83         Foo.prototype = {};
84
85     Because the prototype reassignment destroys the
86     constructor property.
87
88     This will work for all objects that were created using
89     `Object.create` implemented with this library.
90
91 * /!\ Object.getOwnPropertyNames
92
93     This method uses Object.keys, so it will not be accurate
94     on legacy engines.
95
96 * Object.isSealed
97
98     Returns "false" in all legacy engines for all objects,
99     which is conveniently guaranteed to be accurate.
100
101 * Object.isFrozen
102
103     Returns "false" in all legacy engines for all objects,
104     which is conveniently guaranteed to be accurate.
105
106 * Object.isExtensible
107
108     Works like a charm, by trying very hard to extend the
109     object then redacting the extension.
110
111 ### Fail silently
112
113 * /!\ Object.getOwnPropertyDescriptor
114     
115     The behavior of this shim does not conform to ES5.  It
116     should probably not be used at this time, until its
117     behavior has been reviewed and been confirmed to be
118     useful in legacy engines.
119
120 * /!\ Object.defineProperty
121
122     This method will silently fail to set "writable",
123     "enumerable", and "configurable" properties.
124     
125     Providing a getter or setter with "get" or "set" on a
126     descriptor will silently fail on engines that lack
127     "__defineGetter__" and "__defineSetter__", which include
128     all versions of IE up to version 8 so far.
129
130     IE 8 provides a version of this method but it only works
131     on DOM objects.  Thus, the shim will not get installed
132     and attempts to set "value" properties will fail
133     silently on non-DOM objects.
134
135     https://github.com/kriskowal/es5-shim/issues#issue/5
136
137 * /!\ Object.defineProperties
138
139     This uses the Object.defineProperty shim
140
141 * Object.seal
142
143     Silently fails on all legacy engines.  This should be
144     fine unless you are depending on the safety and security
145     provisions of this method, which you cannot possibly
146     obtain in legacy engines.
147
148 * Object.freeze
149
150     Silently fails on all legacy engines.  This should be
151     fine unless you are depending on the safety and security
152     provisions of this method, which you cannot possibly
153     obtain in legacy engines.
154
155 * Object.preventExtensions
156
157     Silently fails on all legacy engines.  This should be
158     fine unless you are depending on the safety and security
159     provisions of this method, which you cannot possibly
160     obtain in legacy engines.
161