@@ -28,12 +28,165 @@ describe('Testing different implementation concepts', () => {
2828 } )
2929 } )
3030
31- describe ( '' , ( ) => {
31+ describe ( 'check how proxy with class works' , ( ) => {
32+ class Class {
33+ constructor ( ) {
34+ console . log ( 'CALLED - Class constructor' )
35+ }
36+ }
37+ let C = new Proxy ( Class , {
38+ construct : function ( target , argumentsList , newTarget ) {
39+ console . log ( 'CALLED - Proxy Class constructor' )
40+ return Reflect . construct ( target , argumentsList )
41+ } ,
42+ apply : function ( target , thisArg , argumentsList ) {
43+ console . log ( 'proxy called - apply' )
44+ // return new target(argumentsList)
45+ }
46+
47+ } )
48+
49+ it ( 'static class can a proxy' , ( ) => {
50+ console . log ( new C ( ) )
51+ } )
52+ } )
53+
54+ describe ( 'Is proxy on superclass constructor called through subclass is instantiated ?' , ( ) => {
55+ class Superclass {
56+ constructor ( ) {
57+ console . log ( 'CALLED - Superclass constructor' )
58+ }
59+ }
60+ let S = new Proxy ( Superclass , {
61+ construct : function ( target , argumentsList , newTarget ) {
62+ console . log ( 'CALLED - Proxy Superclass constructor' )
63+ let instance = Reflect . construct ( target , argumentsList )
64+ return instance
65+ } ,
66+ // apply: function(target, thisArg, argumentsList) {
67+ // console.log('proxy called - apply')
68+ // // return new target(argumentsList)
69+ // }
70+
71+ } )
72+ class Class extends S {
73+ constructor ( ) {
74+ console . log ( 'CALLED - Class constructor' )
75+ super ( )
76+ }
77+ }
78+ it ( 'static class can extend a proxy superclass' , ( ) => {
79+ console . log ( new Class ( ) )
80+ // console.log(new S())
81+ } )
82+ } )
83+
84+ describe ( 'Wrapping "new" calls with a specific Prototype context to share common state' , ( ) => {
85+ class Superclass {
86+ constructor ( ) { }
87+ }
88+ class Class extends Superclass {
89+ constructor ( ) {
90+ super ( )
91+ }
92+ }
93+ Class . prototype . x = 'hello'
94+ let regularInstance = new Class ( )
95+
96+ it ( 'should create regular prototype chain (Basic language behavior)' , ( ) => {
97+ assert . strictEqual ( regularInstance . __proto__ , Class . prototype )
98+ assert . strictEqual ( regularInstance . __proto__ . __proto__ , Superclass . prototype )
99+ } )
32100
101+ function createPrototypeChainObject ( currectPrototype , superPrototype = Object ) {
102+ let pointerPrototype = Object . create ( superPrototype )
103+ return new Proxy ( pointerPrototype , {
104+ get : function ( target , property , receiver ) {
105+ Object . defineProperty ( pointerPrototype , 'delegatedPrototype' , {
106+ value : currectPrototype ,
107+ writable : true ,
108+ enumerable : true ,
109+ configurable : true
110+ } )
111+ if ( property == 'delegatedPrototype' ) return currectPrototype
112+ if ( currectPrototype . hasOwnProperty ( property ) ) {
113+ return Reflect . get ( currectPrototype , property )
114+ } else if ( Object . getPrototypeOf ( target ) ) {
115+ return Reflect . get ( Object . getPrototypeOf ( target ) , property )
116+ } else {
117+ return undefined
118+ }
119+ } ,
120+ // getPrototypeOf(target) {
121+ // console.log(target)
122+ // return Object.getPrototypeOf(target)
123+ // }
124+ } )
125+ }
126+ // Manual prototype switching
127+ let superclass = new Superclass ( )
128+ superclass . sharedState = 'Shared'
129+ let sharedStateInstance1 = new Class ( )
130+ let superclassProto = createPrototypeChainObject ( superclass )
131+ let classProto = createPrototypeChainObject ( Class . prototype , superclassProto )
132+ Object . setPrototypeOf ( sharedStateInstance1 , classProto )
133+ let superclassProto2 = createPrototypeChainObject ( superclass )
134+ let classProto2 = createPrototypeChainObject ( Class . prototype , superclassProto2 )
135+ let sharedStateInstance2 = new Class ( )
136+ Object . setPrototypeOf ( sharedStateInstance2 , classProto2 )
137+
138+ it ( 'should create shared prototype chain' , ( ) => {
139+ assert . strictEqual ( sharedStateInstance1 . x , Class . prototype . x )
140+ assert . strictEqual ( sharedStateInstance1 . sharedState , superclass . sharedState )
141+ assert . strictEqual ( sharedStateInstance1 . sharedState , sharedStateInstance2 . sharedState )
142+ // assert.strictEqual(sharedStateInstance1.__proto__.__proto__.__proto__, Superclass.prototype)
143+ assert . strictEqual ( Object . getPrototypeOf ( sharedStateInstance1 ) . delegatedPrototype , Object . getPrototypeOf ( sharedStateInstance2 ) . delegatedPrototype )
144+
145+ } )
146+
147+ function createPrototypeChainObjectOnConstructor ( Class , Superclass ) {
148+ return new Proxy ( Class , {
149+ construct : function ( newObj , argumentsList , constructorFunc ) {
150+ let oldProto = Class . prototype
151+ let newProto ;
152+ if ( Object . getPrototypeOf ( Class . prototype ) !== Object ||
153+ Object . getPrototypeOf ( Class . prototype ) !== Function ||
154+ Object . getPrototypeOf ( Class . prototype ) !== Object . prototype ||
155+ Object . getPrototypeOf ( Class . prototype ) !== Function . prototype ||
156+ Object . getPrototypeOf ( Class . prototype ) !== null ) {
157+ newProto = createPrototypeChainObject ( oldProto , Superclass || Object . getPrototypeOf ( Class . prototype ) )
158+ } else {
159+ newProto = createPrototypeChainObject ( oldProto )
160+ }
161+ Object . setPrototypeOf ( newObj , newProto )
162+ return newObj
163+ } ,
164+ // getPrototypeOf(target) {
165+
166+ // }
167+ } )
168+ }
169+
170+ class S { }
171+ S . prototype . x = 'x'
172+ let Sp = createPrototypeChainObjectOnConstructor ( S )
173+ class C extends Sp { }
174+ let Cp = createPrototypeChainObjectOnConstructor ( C , Sp )
175+ // new S => {}.proto = {} - sp - Sp
176+
177+ let instancex = new Sp ( )
178+ let instancey = new Cp ( )
179+ // console.log(Object.getPrototypeOf(instancey).delegatedPrototype)
180+ // console.log(Object.getPrototypeOf(Object.getPrototypeOf(instancey)).delegatedPrototype)
181+ // console.log(Object.getPrototypeOf(instancey))
182+ // console.log(Object.getPrototypeOf(Object.getPrototypeOf(Object.getPrototypeOf(instancey))))
183+
184+
185+
33186 } )
34187
35188} )
36189
37- setTimeout ( ( ) => {
190+ // setTimeout(() => {
38191
39- } , 10000000000 ) ;
192+ // }, 10000000000);
0 commit comments