14
14
var ReactCurrentOwner = require ( 'ReactCurrentOwner' ) ;
15
15
16
16
var assign = require ( 'Object.assign' ) ;
17
- var warning = require ( 'warning' ) ;
18
17
19
18
var RESERVED_PROPS = {
20
19
key : true ,
21
20
ref : true ,
22
21
} ;
23
22
24
- /**
25
- * Warn for mutations.
26
- *
27
- * @internal
28
- * @param {object } object
29
- * @param {string } key
30
- */
31
- function defineWarningProperty ( object , key ) {
32
- Object . defineProperty ( object , key , {
33
-
34
- configurable : false ,
35
- enumerable : true ,
36
-
37
- get : function ( ) {
38
- if ( ! this . _store ) {
39
- return null ;
40
- }
41
- return this . _store [ key ] ;
42
- } ,
43
-
44
- set : function ( value ) {
45
- warning (
46
- false ,
47
- 'Don\'t set the %s property of the React element. Instead, ' +
48
- 'specify the correct value when initially creating the element.' ,
49
- key
50
- ) ;
51
- this . _store [ key ] = value ;
52
- } ,
53
-
54
- } ) ;
55
- }
56
-
57
- /**
58
- * This is updated to true if the membrane is successfully created.
59
- */
60
- var useMutationMembrane = false ;
61
-
62
- /**
63
- * Warn for mutations.
64
- *
65
- * @internal
66
- * @param {object } element
67
- */
68
- function defineMutationMembrane ( prototype ) {
23
+ var canDefineProperty = false ;
24
+ if ( __DEV__ ) {
69
25
try {
70
- var pseudoFrozenProperties = {
71
- props : true ,
72
- } ;
73
- for ( var key in pseudoFrozenProperties ) {
74
- defineWarningProperty ( prototype , key ) ;
75
- }
76
- useMutationMembrane = true ;
26
+ Object . defineProperty ( { } , 'x' , { } ) ;
27
+ canDefineProperty = true ;
77
28
} catch ( x ) {
78
29
// IE will fail on defineProperty
79
30
}
@@ -91,57 +42,46 @@ function defineMutationMembrane(prototype) {
91
42
* @internal
92
43
*/
93
44
var ReactElement = function ( type , key , ref , owner , props ) {
94
- // Built-in properties that belong on the element
95
- this . type = type ;
96
- this . key = key ;
97
- this . ref = ref ;
45
+ var element = {
46
+ // Built-in properties that belong on the element
47
+ type : type ,
48
+ key : key ,
49
+ ref : ref ,
98
50
99
- // Record the component responsible for creating this element.
100
- this . _owner = owner ;
51
+ // Record the component responsible for creating this element.
52
+ _owner : owner ,
53
+
54
+ props : props ,
55
+
56
+ _isReactElement : true
57
+ } ;
101
58
102
59
if ( __DEV__ ) {
103
60
// The validation flag and props are currently mutative. We put them on
104
61
// an external backing store so that we can freeze the whole object.
105
62
// This can be replaced with a WeakMap once they are implemented in
106
63
// commonly used development environments.
107
- this . _store = { props : props , originalProps : assign ( { } , props ) } ;
64
+ element . _store = { originalProps : assign ( { } , props ) } ;
108
65
109
66
// To make comparing ReactElements easier for testing purposes, we make
110
67
// the validation flag non-enumerable (where possible, which should
111
68
// include every environment we run tests in), so the test framework
112
69
// ignores it.
113
- try {
114
- Object . defineProperty ( this . _store , 'validated' , {
70
+ if ( canDefineProperty ) {
71
+ Object . defineProperty ( element . _store , 'validated' , {
115
72
configurable : false ,
116
73
enumerable : false ,
117
74
writable : true ,
118
75
} ) ;
119
- } catch ( x ) {
120
- }
121
- this . _store . validated = false ;
122
-
123
- // We're not allowed to set props directly on the object so we early
124
- // return and rely on the prototype membrane to forward to the backing
125
- // store.
126
- if ( useMutationMembrane ) {
127
- Object . freeze ( this ) ;
128
- return ;
129
76
}
130
- }
77
+ element . _store . validated = false ;
131
78
132
- this . props = props ;
133
- } ;
79
+ Object . freeze ( element ) ;
80
+ }
134
81
135
- // We intentionally don't expose the function on the constructor property.
136
- // ReactElement should be indistinguishable from a plain object.
137
- ReactElement . prototype = {
138
- _isReactElement : true ,
82
+ return element ;
139
83
} ;
140
84
141
- if ( __DEV__ ) {
142
- defineMutationMembrane ( ReactElement . prototype ) ;
143
- }
144
-
145
85
ReactElement . createElement = function ( type , config , children ) {
146
86
var propName ;
147
87
@@ -186,7 +126,7 @@ ReactElement.createElement = function(type, config, children) {
186
126
}
187
127
}
188
128
189
- return new ReactElement (
129
+ return ReactElement (
190
130
type ,
191
131
key ,
192
132
ref ,
@@ -207,7 +147,7 @@ ReactElement.createFactory = function(type) {
207
147
} ;
208
148
209
149
ReactElement . cloneAndReplaceProps = function ( oldElement , newProps ) {
210
- var newElement = new ReactElement (
150
+ var newElement = ReactElement (
211
151
oldElement . type ,
212
152
oldElement . key ,
213
153
oldElement . ref ,
@@ -266,7 +206,7 @@ ReactElement.cloneElement = function(element, config, children) {
266
206
props . children = childArray ;
267
207
}
268
208
269
- return new ReactElement (
209
+ return ReactElement (
270
210
element . type ,
271
211
key ,
272
212
ref ,
0 commit comments