-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
core_object.js
1082 lines (869 loc) · 28.9 KB
/
core_object.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
@module @ember/object
*/
// using ember-metal/lib/main here to ensure that ember-debug is setup
// if present
import { FACTORY_FOR } from 'container';
import {
assign,
guidFor,
getName,
setName,
makeArray,
HAS_NATIVE_PROXY,
isInternalSymbol,
} from 'ember-utils';
import {
PROXY_CONTENT,
descriptorFor,
meta,
peekMeta,
finishChains,
sendEvent,
Mixin,
defineProperty,
ComputedProperty,
InjectedProperty,
schedule,
deleteMeta,
descriptor,
classToString,
} from 'ember-metal';
import ActionHandler from '../mixins/action_handler';
import { validatePropertyInjections } from '../inject';
import { assert } from 'ember-debug';
import { DEBUG } from 'ember-env-flags';
import { ENV } from 'ember-environment';
import { MANDATORY_GETTER, MANDATORY_SETTER, EMBER_METAL_ES5_GETTERS } from 'ember/features';
const applyMixin = Mixin._apply;
const reopen = Mixin.prototype.reopen;
function makeCtor(base) {
// Note: avoid accessing any properties on the object since it makes the
// method a lot faster. This is glue code so we want it to be as fast as
// possible.
let wasApplied = false;
let Class;
if (base) {
Class = class extends base {
constructor(properties) {
if (!wasApplied) {
Class.proto(); // prepare prototype...
}
super(properties);
}
};
} else {
let initFactory;
Class = class {
constructor(properties) {
if (!wasApplied) {
Class.proto(); // prepare prototype...
}
let self = this;
if (initFactory !== void 0) {
FACTORY_FOR.set(this, initFactory);
initFactory = void 0;
}
let beforeInitCalled; // only used in debug builds to enable the proxy trap
// using DEBUG here to avoid the extraneous variable when not needed
if (DEBUG) {
beforeInitCalled = true;
}
if (
DEBUG &&
MANDATORY_GETTER &&
EMBER_METAL_ES5_GETTERS &&
HAS_NATIVE_PROXY &&
typeof self.unknownProperty === 'function'
) {
let messageFor = (obj, property) => {
return (
`You attempted to access the \`${String(property)}\` property (of ${obj}).\n` +
`Since Ember 3.1, this is usually fine as you no longer need to use \`.get()\`\n` +
`to access computed properties. However, in this case, the object in question\n` +
`is a special kind of Ember object (a proxy). Therefore, it is still necessary\n` +
`to use \`.get('${String(property)}')\` in this case.\n\n` +
`If you encountered this error because of third-party code that you don't control,\n` +
`there is more information at https://github.com/emberjs/ember.js/issues/16148, and\n` +
`you can help us improve this error message by telling us more about what happened in\n` +
`this situation.`
);
};
/* globals Proxy Reflect */
self = new Proxy(this, {
get(target, property, receiver) {
if (property === PROXY_CONTENT) {
return target;
} else if (
beforeInitCalled ||
typeof property === 'symbol' ||
isInternalSymbol(property) ||
property === 'toJSON' ||
property === 'toString' ||
property === 'toStringExtension' ||
property === 'didDefineProperty' ||
property === 'willWatchProperty' ||
property === 'didUnwatchProperty' ||
property === 'didAddListener' ||
property === 'didRemoveListener' ||
property === '__DESCRIPTOR__' ||
property === 'isDescriptor' ||
property in target
) {
return Reflect.get(target, property, receiver);
}
let value = target.unknownProperty.call(receiver, property);
assert(messageFor(receiver, property), value === undefined);
},
});
}
let m = meta(self);
let proto = m.proto;
m.proto = self;
if (properties !== undefined) {
assert(
'EmberObject.create only accepts objects.',
typeof properties === 'object' && properties !== null
);
assert(
'EmberObject.create no longer supports mixing in other ' +
'definitions, use .extend & .create separately instead.',
!(properties instanceof Mixin)
);
let concatenatedProperties = self.concatenatedProperties;
let mergedProperties = self.mergedProperties;
let hasConcatenatedProps =
concatenatedProperties !== undefined && concatenatedProperties.length > 0;
let hasMergedProps = mergedProperties !== undefined && mergedProperties.length > 0;
let keyNames = Object.keys(properties);
for (let i = 0; i < keyNames.length; i++) {
let keyName = keyNames[i];
let value = properties[keyName];
if (ENV._ENABLE_BINDING_SUPPORT && Mixin.detectBinding(keyName)) {
m.writeBindings(keyName, value);
}
assert(
'EmberObject.create no longer supports defining computed ' +
'properties. Define computed properties using extend() or reopen() ' +
'before calling create().',
!(value instanceof ComputedProperty)
);
assert(
'EmberObject.create no longer supports defining methods that call _super.',
!(typeof value === 'function' && value.toString().indexOf('._super') !== -1)
);
assert(
'`actions` must be provided at extend time, not at create time, ' +
'when Ember.ActionHandler is used (i.e. views, controllers & routes).',
!(keyName === 'actions' && ActionHandler.detect(this))
);
let possibleDesc = descriptorFor(self, keyName, m);
let isDescriptor = possibleDesc !== undefined;
if (!isDescriptor) {
let baseValue = self[keyName];
if (hasConcatenatedProps && concatenatedProperties.indexOf(keyName) > -1) {
if (baseValue) {
value = makeArray(baseValue).concat(value);
} else {
value = makeArray(value);
}
}
if (hasMergedProps && mergedProperties.indexOf(keyName) > -1) {
value = assign({}, baseValue, value);
}
}
if (isDescriptor) {
possibleDesc.set(self, keyName, value);
} else if (typeof self.setUnknownProperty === 'function' && !(keyName in self)) {
self.setUnknownProperty(keyName, value);
} else {
if (MANDATORY_SETTER) {
defineProperty(self, keyName, null, value); // setup mandatory setter
} else {
self[keyName] = value;
}
}
}
}
if (ENV._ENABLE_BINDING_SUPPORT) {
Mixin.finishPartial(self, m);
}
// using DEBUG here to avoid the extraneous variable when not needed
if (DEBUG) {
beforeInitCalled = false;
}
self.init(...arguments);
m.proto = proto;
finishChains(m);
sendEvent(self, 'init', undefined, undefined, undefined, m);
// only return when in debug builds and `self` is the proxy created above
if (DEBUG && self !== this) {
return self;
}
}
static _initFactory(factory) {
initFactory = factory;
}
};
}
Class.willReopen = () => {
if (wasApplied) {
Class.PrototypeMixin = Mixin.create(Class.PrototypeMixin);
}
wasApplied = false;
};
Class.proto = () => {
let superclass = Class.superclass;
if (superclass) {
superclass.proto();
}
if (!wasApplied) {
wasApplied = true;
Class.PrototypeMixin.applyPartial(Class.prototype);
}
return Class.prototype;
};
return Class;
}
const IS_DESTROYED = descriptor({
configurable: true,
enumerable: false,
get() {
return peekMeta(this).isSourceDestroyed();
},
set(value) {
assert(
`You cannot set \`${this}.isDestroyed\` directly, please use \`.destroy()\`.`,
value === IS_DESTROYED
);
},
});
const IS_DESTROYING = descriptor({
configurable: true,
enumerable: false,
get() {
return peekMeta(this).isSourceDestroying();
},
set(value) {
assert(
`You cannot set \`${this}.isDestroying\` directly, please use \`.destroy()\`.`,
value === IS_DESTROYING
);
},
});
/**
@class CoreObject
@public
*/
let CoreObject = makeCtor();
CoreObject.prototype.toString = classToString;
CoreObject.toString = classToString;
setName(CoreObject, 'Ember.CoreObject');
CoreObject.PrototypeMixin = Mixin.create({
reopen(...args) {
applyMixin(this, args, true);
return this;
},
/**
An overridable method called when objects are instantiated. By default,
does nothing unless it is overridden during class definition.
Example:
```javascript
import EmberObject from '@ember/object';
const Person = EmberObject.extend({
init() {
alert(`Name is ${this.get('name')}`);
}
});
let steve = Person.create({
name: 'Steve'
});
// alerts 'Name is Steve'.
```
NOTE: If you do override `init` for a framework class like `Ember.View`,
be sure to call `this._super(...arguments)` in your
`init` declaration! If you don't, Ember may not have an opportunity to
do important setup work, and you'll see strange behavior in your
application.
@method init
@public
*/
init() {},
/**
Defines the properties that will be concatenated from the superclass
(instead of overridden).
By default, when you extend an Ember class a property defined in
the subclass overrides a property with the same name that is defined
in the superclass. However, there are some cases where it is preferable
to build up a property's value by combining the superclass' property
value with the subclass' value. An example of this in use within Ember
is the `classNames` property of `Ember.View`.
Here is some sample code showing the difference between a concatenated
property and a normal one:
```javascript
import EmberObject from '@ember/object';
const Bar = EmberObject.extend({
// Configure which properties to concatenate
concatenatedProperties: ['concatenatedProperty'],
someNonConcatenatedProperty: ['bar'],
concatenatedProperty: ['bar']
});
const FooBar = Bar.extend({
someNonConcatenatedProperty: ['foo'],
concatenatedProperty: ['foo']
});
let fooBar = FooBar.create();
fooBar.get('someNonConcatenatedProperty'); // ['foo']
fooBar.get('concatenatedProperty'); // ['bar', 'foo']
```
This behavior extends to object creation as well. Continuing the
above example:
```javascript
let fooBar = FooBar.create({
someNonConcatenatedProperty: ['baz'],
concatenatedProperty: ['baz']
})
fooBar.get('someNonConcatenatedProperty'); // ['baz']
fooBar.get('concatenatedProperty'); // ['bar', 'foo', 'baz']
```
Adding a single property that is not an array will just add it in the array:
```javascript
let fooBar = FooBar.create({
concatenatedProperty: 'baz'
})
view.get('concatenatedProperty'); // ['bar', 'foo', 'baz']
```
Using the `concatenatedProperties` property, we can tell Ember to mix the
content of the properties.
In `Component` the `classNames`, `classNameBindings` and
`attributeBindings` properties are concatenated.
This feature is available for you to use throughout the Ember object model,
although typical app developers are likely to use it infrequently. Since
it changes expectations about behavior of properties, you should properly
document its usage in each individual concatenated property (to not
mislead your users to think they can override the property in a subclass).
@property concatenatedProperties
@type Array
@default null
@public
*/
concatenatedProperties: null,
/**
Defines the properties that will be merged from the superclass
(instead of overridden).
By default, when you extend an Ember class a property defined in
the subclass overrides a property with the same name that is defined
in the superclass. However, there are some cases where it is preferable
to build up a property's value by merging the superclass property value
with the subclass property's value. An example of this in use within Ember
is the `queryParams` property of routes.
Here is some sample code showing the difference between a merged
property and a normal one:
```javascript
import EmberObject from '@ember/object';
const Bar = EmberObject.extend({
// Configure which properties are to be merged
mergedProperties: ['mergedProperty'],
someNonMergedProperty: {
nonMerged: 'superclass value of nonMerged'
},
mergedProperty: {
page: { replace: false },
limit: { replace: true }
}
});
const FooBar = Bar.extend({
someNonMergedProperty: {
completelyNonMerged: 'subclass value of nonMerged'
},
mergedProperty: {
limit: { replace: false }
}
});
let fooBar = FooBar.create();
fooBar.get('someNonMergedProperty');
// => { completelyNonMerged: 'subclass value of nonMerged' }
//
// Note the entire object, including the nonMerged property of
// the superclass object, has been replaced
fooBar.get('mergedProperty');
// => {
// page: {replace: false},
// limit: {replace: false}
// }
//
// Note the page remains from the superclass, and the
// `limit` property's value of `false` has been merged from
// the subclass.
```
This behavior is not available during object `create` calls. It is only
available at `extend` time.
In `Route` the `queryParams` property is merged.
This feature is available for you to use throughout the Ember object model,
although typical app developers are likely to use it infrequently. Since
it changes expectations about behavior of properties, you should properly
document its usage in each individual merged property (to not
mislead your users to think they can override the property in a subclass).
@property mergedProperties
@type Array
@default null
@public
*/
mergedProperties: null,
/**
Destroyed object property flag.
if this property is `true` the observers and bindings were already
removed by the effect of calling the `destroy()` method.
@property isDestroyed
@default false
@public
*/
isDestroyed: IS_DESTROYED,
/**
Destruction scheduled flag. The `destroy()` method has been called.
The object stays intact until the end of the run loop at which point
the `isDestroyed` flag is set.
@property isDestroying
@default false
@public
*/
isDestroying: IS_DESTROYING,
/**
Destroys an object by setting the `isDestroyed` flag and removing its
metadata, which effectively destroys observers and bindings.
If you try to set a property on a destroyed object, an exception will be
raised.
Note that destruction is scheduled for the end of the run loop and does not
happen immediately. It will set an isDestroying flag immediately.
@method destroy
@return {EmberObject} receiver
@public
*/
destroy() {
let m = peekMeta(this);
if (m.isSourceDestroying()) {
return;
}
m.setSourceDestroying();
schedule('actions', this, this.willDestroy);
schedule('destroy', this, this._scheduledDestroy, m);
return this;
},
/**
Override to implement teardown.
@method willDestroy
@public
*/
willDestroy() {},
/**
Invoked by the run loop to actually destroy the object. This is
scheduled for execution by the `destroy` method.
@private
@method _scheduledDestroy
*/
_scheduledDestroy(m) {
if (m.isSourceDestroyed()) {
return;
}
deleteMeta(this);
m.setSourceDestroyed();
},
/**
Returns a string representation which attempts to provide more information
than Javascript's `toString` typically does, in a generic way for all Ember
objects.
```javascript
import EmberObject from '@ember/object';
const Person = EmberObject.extend();
person = Person.create();
person.toString(); //=> "<Person:ember1024>"
```
If the object's class is not defined on an Ember namespace, it will
indicate it is a subclass of the registered superclass:
```javascript
const Student = Person.extend();
let student = Student.create();
student.toString(); //=> "<(subclass of Person):ember1025>"
```
If the method `toStringExtension` is defined, its return value will be
included in the output.
```javascript
const Teacher = Person.extend({
toStringExtension() {
return this.get('fullName');
}
});
teacher = Teacher.create();
teacher.toString(); //=> "<Teacher:ember1026:Tom Dale>"
```
@method toString
@return {String} string representation
@public
*/
toString() {
let hasToStringExtension = typeof this.toStringExtension === 'function';
let extension = hasToStringExtension ? `:${this.toStringExtension()}` : '';
let ret = `<${getName(this) || FACTORY_FOR.get(this) || this.constructor.toString()}:${guidFor(
this
)}${extension}>`;
return ret;
},
});
CoreObject.PrototypeMixin.ownerConstructor = CoreObject;
CoreObject.__super__ = null;
let ClassMixinProps = {
isClass: true,
isMethod: false,
/**
Creates a new subclass.
```javascript
import EmberObject from '@ember/object';
const Person = EmberObject.extend({
say(thing) {
alert(thing);
}
});
```
This defines a new subclass of EmberObject: `Person`. It contains one method: `say()`.
You can also create a subclass from any existing class by calling its `extend()` method.
For example, you might want to create a subclass of Ember's built-in `Component` class:
```javascript
import Component from '@ember/component';
const PersonComponent = Component.extend({
tagName: 'li',
classNameBindings: ['isAdministrator']
});
```
When defining a subclass, you can override methods but still access the
implementation of your parent class by calling the special `_super()` method:
```javascript
import EmberObject from '@ember/object';
const Person = EmberObject.extend({
say(thing) {
let name = this.get('name');
alert(`${name} says: ${thing}`);
}
});
const Soldier = Person.extend({
say(thing) {
this._super(`${thing}, sir!`);
},
march(numberOfHours) {
alert(`${this.get('name')} marches for ${numberOfHours} hours.`);
}
});
let yehuda = Soldier.create({
name: 'Yehuda Katz'
});
yehuda.say('Yes'); // alerts "Yehuda Katz says: Yes, sir!"
```
The `create()` on line #17 creates an *instance* of the `Soldier` class.
The `extend()` on line #8 creates a *subclass* of `Person`. Any instance
of the `Person` class will *not* have the `march()` method.
You can also pass `Mixin` classes to add additional properties to the subclass.
```javascript
import EmberObject from '@ember/object';
import Mixin from '@ember/object/mixin';
const Person = EmberObject.extend({
say(thing) {
alert(`${this.get('name')} says: ${thing}`);
}
});
const SingingMixin = Mixin.create({
sing(thing){
alert(`${this.get('name')} sings: la la la ${thing}`);
}
});
const BroadwayStar = Person.extend(SingingMixin, {
dance() {
alert(`${this.get('name')} dances: tap tap tap tap `);
}
});
```
The `BroadwayStar` class contains three methods: `say()`, `sing()`, and `dance()`.
@method extend
@static
@for @ember/object
@param {Mixin} [mixins]* One or more Mixin classes
@param {Object} [arguments]* Object containing values to use within the new class
@public
*/
extend() {
let Class = makeCtor(this);
Class.ClassMixin = Mixin.create(this.ClassMixin);
Class.PrototypeMixin = Mixin.create(this.PrototypeMixin);
Class.ClassMixin.ownerConstructor = Class;
Class.PrototypeMixin.ownerConstructor = Class;
reopen.apply(Class.PrototypeMixin, arguments);
Class.superclass = this;
Class.__super__ = this.prototype;
let proto = Class.prototype;
meta(proto).proto = proto; // this will disable observers on prototype
Class.ClassMixin.apply(Class);
return Class;
},
/**
Creates an instance of a class. Accepts either no arguments, or an object
containing values to initialize the newly instantiated object with.
```javascript
import EmberObject from '@ember/object';
const Person = EmberObject.extend({
helloWorld() {
alert(`Hi, my name is ${this.get('name')}`);
}
});
let tom = Person.create({
name: 'Tom Dale'
});
tom.helloWorld(); // alerts "Hi, my name is Tom Dale".
```
`create` will call the `init` function if defined during
`AnyObject.extend`
If no arguments are passed to `create`, it will not set values to the new
instance during initialization:
```javascript
let noName = Person.create();
noName.helloWorld(); // alerts undefined
```
NOTE: For performance reasons, you cannot declare methods or computed
properties during `create`. You should instead declare methods and computed
properties when using `extend`.
@method create
@for @ember/object
@static
@param [arguments]*
@public
*/
create(props, extra) {
let C = this;
if (extra === undefined) {
return new C(props);
} else {
return new C(flattenProps.apply(this, arguments));
}
},
/**
Augments a constructor's prototype with additional
properties and functions:
```javascript
import EmberObject from '@ember/object';
const MyObject = EmberObject.extend({
name: 'an object'
});
o = MyObject.create();
o.get('name'); // 'an object'
MyObject.reopen({
say(msg) {
console.log(msg);
}
});
o2 = MyObject.create();
o2.say('hello'); // logs "hello"
o.say('goodbye'); // logs "goodbye"
```
To add functions and properties to the constructor itself,
see `reopenClass`
@method reopen
@for @ember/object
@static
@public
*/
reopen() {
this.willReopen();
reopen.apply(this.PrototypeMixin, arguments);
return this;
},
/**
Augments a constructor's own properties and functions:
```javascript
import EmberObject from '@ember/object';
const MyObject = EmberObject.extend({
name: 'an object'
});
MyObject.reopenClass({
canBuild: false
});
MyObject.canBuild; // false
o = MyObject.create();
```
In other words, this creates static properties and functions for the class.
These are only available on the class and not on any instance of that class.
```javascript
import EmberObject from '@ember/object';
const Person = EmberObject.extend({
name: '',
sayHello() {
alert(`Hello. My name is ${this.get('name')}`);
}
});
Person.reopenClass({
species: 'Homo sapiens',
createPerson(name) {
return Person.create({ name });
}
});
let tom = Person.create({
name: 'Tom Dale'
});
let yehuda = Person.createPerson('Yehuda Katz');
tom.sayHello(); // "Hello. My name is Tom Dale"
yehuda.sayHello(); // "Hello. My name is Yehuda Katz"
alert(Person.species); // "Homo sapiens"
```
Note that `species` and `createPerson` are *not* valid on the `tom` and `yehuda`
variables. They are only valid on `Person`.
To add functions and properties to instances of
a constructor by extending the constructor's prototype
see `reopen`
@method reopenClass
@for @ember/object
@static
@public
*/
reopenClass() {
reopen.apply(this.ClassMixin, arguments);
applyMixin(this, arguments, false);
return this;
},
detect(obj) {
if ('function' !== typeof obj) {
return false;
}
while (obj) {
if (obj === this) {
return true;
}
obj = obj.superclass;
}
return false;
},
detectInstance(obj) {
return obj instanceof this;
},
/**
In some cases, you may want to annotate computed properties with additional
metadata about how they function or what values they operate on. For
example, computed property functions may close over variables that are then
no longer available for introspection.
You can pass a hash of these values to a computed property like this:
```javascript
import { computed } from '@ember/object';
person: computed(function() {
let personId = this.get('personId');
return Person.create({ id: personId });
}).meta({ type: Person })
```
Once you've done this, you can retrieve the values saved to the computed
property from your class like this:
```javascript
MyClass.metaForProperty('person');
```
This will return the original hash that was passed to `meta()`.
@static
@method metaForProperty
@param key {String} property name
@private
*/
metaForProperty(key) {
let proto = this.proto(); // ensure prototype is initialized
let possibleDesc = descriptorFor(proto, key);
assert(
`metaForProperty() could not find a computed property with key '${key}'.`,
possibleDesc !== undefined
);
return possibleDesc._meta || {};
},
/**
Iterate over each computed property for the class, passing its name
and any associated metadata (see `metaForProperty`) to the callback.
@static
@method eachComputedProperty
@param {Function} callback
@param {Object} binding
@private
*/
eachComputedProperty(callback, binding = this) {
this.proto(); // ensure prototype is initialized
let empty = {};
meta(this.prototype).forEachDescriptors((name, descriptor) => {
if (descriptor.enumerable) {
let meta = descriptor._meta || empty;
callback.call(binding, name, meta);
}
});
},
};
function injectedPropertyAssertion() {
assert('Injected properties are invalid', validatePropertyInjections(this));
}
function flattenProps(...props) {
let { concatenatedProperties, mergedProperties } = this;
let hasConcatenatedProps =
concatenatedProperties !== undefined && concatenatedProperties.length > 0;
let hasMergedProps = mergedProperties !== undefined && mergedProperties.length > 0;
let initProperties = {};
for (let i = 0; i < props.length; i++) {