Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #35936

factory pattern, seems like object property gets overwritten somehow

Newsgroups comp.lang.javascript
Date 2018-12-30 10:35 -0800
Message-ID <511ba0cd-7b28-48c8-821d-166b45a22a62@googlegroups.com> (permalink)
Subject factory pattern, seems like object property gets overwritten somehow
From Matt <msg2maciej@gmail.com>

Show all headers | View raw


Simple static page, you can copy\paste code below, and open it in browser.
I'm not pro-coder, please tell me where I do mistake? Seems like Object's property *index* is being overwritten, I do not understand why?
Why this code is not working:


<!DOCTYPE html><html><head><meta charset="UTF-8"><title>TEST</title></head><body>

<script>
// ----------------------------------------------------------------------------------------------------
const FactoryA = (function () {
	// --------------------------
	const FactoryA_Prototype = {
		toString: function() { return 'FactoryA'; }
	};

	// --------------------------
	return function () {
		const Oc_FactoryA = Object.create (FactoryA_Prototype, {

			index: {
				set:  function(x) { console.log('FactoryA.set:= '+x); index = x; },
				get:  function() { console.log('FactoryA.get:= '+index); return index; }
			}

		});
		Oc_FactoryA.index = null;
		return Object.freeze (Oc_FactoryA);
	};
	// --------------------------
})();
// ----------------------------------------------------------------------------------------------------
const FactoryB = (function () {
	// --------------------------
	const FactoryB_Prototype = {
		toString: function() { return 'FactoryB'; }
	};

	// --------------------------
	return function () {
		const Oc_FactoryB = Object.create (FactoryB_Prototype, {

			index: {
				set:  function(x) { console.log('FactoryB.set:= '+x); index = x; },
				get:  function() {

				console.log('FactoryB.get index should be = 1, and got = '+index+'    <- OK');
				FactoryA();
				console.log('FactoryB.get index should be = 1, and got = '+index+'    <- NOT OK !!, is that because *index* is overwritten somehow?');

				return index;
				}
			}

		});
		Oc_FactoryB.index = null;
		return Object.freeze (Oc_FactoryB);
	};
	// --------------------------
})();
// ----------------------------------------------------------------------------------------------------

const runit = function() {

const b = FactoryB();
b.index = 1;
const test = b.index;

}; runit();


</script>
</body>
</html>

Back to comp.lang.javascript | Previous | Next — Next in thread | Find similar | Unroll thread


Thread

factory pattern, seems like object property gets overwritten somehow Matt <msg2maciej@gmail.com> - 2018-12-30 10:35 -0800
  Re: factory pattern, seems like object property gets overwritten somehow Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2018-12-30 22:30 +0100
    Re: factory pattern, seems like object property gets overwritten somehow Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2018-12-30 22:46 +0100
      Re: factory pattern, seems like object property gets overwritten somehow Matt <msg2maciej@gmail.com> - 2019-01-06 19:15 -0800
        Re: factory pattern, seems like object property gets overwritten somehow Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2019-01-11 00:56 +0100
    Re: factory pattern, seems like object property gets overwritten somehow "R.Wieser" <address@not.available> - 2018-12-31 10:07 +0100

csiph-web