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


Groups > comp.lang.javascript > #35936 > unrolled thread

factory pattern, seems like object property gets overwritten somehow

Started byMatt <msg2maciej@gmail.com>
First post2018-12-30 10:35 -0800
Last post2018-12-31 10:07 +0100
Articles 6 — 3 participants

Back to article view | Back to comp.lang.javascript


Contents

  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

#35936 — factory pattern, seems like object property gets overwritten somehow

FromMatt <msg2maciej@gmail.com>
Date2018-12-30 10:35 -0800
Subjectfactory pattern, seems like object property gets overwritten somehow
Message-ID<511ba0cd-7b28-48c8-821d-166b45a22a62@googlegroups.com>
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>

[toc] | [next] | [standalone]


#35937

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2018-12-30 22:30 +0100
Message-ID<b780b412-9227-9f24-e568-057ddc612943@PointedEars.de>
In reply to#35936
Matt wrote:
^^^^
Your full real name belongs there.

> I'm not pro-coder, please tell me where I do mistake? Seems like Object's
> property *index* is being overwritten,

It is not.  “Object” refers to a completely different object.

> I do not understand why? Why this code is not working:
Even a “not pro-coder” should know what a debugger is.  Web browsers have it
built-in these days.  Press F12.

> // ----------------------------------------------------------------------------------------------------

Use single-line comments for deactivating lines of code, multi-line comments
for documenting them.

> const FactoryA = (function () {

It is a matter for debate whether this identifier should start with an
uppercase character, as it does not refer to a constructor.

> 	// --------------------------

This delimiter is pointless.  Remove it.

Do not use tabs for indentation, use 2 spaces.

> 	const FactoryA_Prototype = {
> 		toString: function() { return 'FactoryA'; }

In ES 6+ (which introduces “const” also), this is better written

    /* method declaration shorthand */
    toString () { return 'FactoryA'; }

or

    /* property declaration with arrow function shorthand */
    toString: () => 'FactoryA'

Keep in mind that you might need a transpiler to generate browser-compatible
code from ES 6+ code.

> 	};
> 
> 	// --------------------------

See above.

> 	return function () {
> 		const Oc_FactoryA = Object.create (FactoryA_Prototype, {
                                           ^^^^^^^^^^^^
> 
> 			index: {
> 				set:  function(x) { console.log('FactoryA.set:= '+x); index = x; },
                                            ^^^^^^^^^^^^^^^^^^^^^^^
    [Develop a consistent code style.  Start with that which is recommended
     by Airbnb.  (The following are my best practice recommendations which
     partially contradict that.)

     If you can avoid it, do NOT write whitespace before the opening
     parenthesis of a function/method call.

     Do write *one* before the declaration of an argument list, to tell both
     apart easier.

     Write only *one* statement per line (a good rule of thumb for other
     programming languages, too).]

Here you are setting some “index” variable/property that is not defined in
this scope.

This will create a property of the global object in non-strict mode and
throw a ReferenceError exception in strict mode.

Always write and test programs in strict mode unless you have a good reason
not to:

    set: function (x) {
      'use strict';

      console.log('FactoryA.set :=', x);

      /* throws ReferenceError exception */
      index = x;
    }

When this part is working, move the 'use strict' to the next outer scope
until it is at the global scope.  If you are using ES 6 modules, you can
simply keep it there; if not, wrap everything (including the strict mode
declaration) into

  (function (global) {
    // …
  }(this));

Keep in mind that you have to use “global” then to refer to the global
object as you are then in function (local) scope.

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

Here you are reading the same global property that you set previously.

> 			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?');

Same problem as above.

> […]

This code is very complicated; at first glance it looks hopelessly
*over*complicated.  What’s the point?

If you are “not pro-coder”, why do you not start with simpler code?

-- 
PointedEars

Twitter: @PointedEars2
Please do not cc me. / Bitte keine Kopien per E-Mail.

[toc] | [prev] | [next] | [standalone]


#35938

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2018-12-30 22:46 +0100
Message-ID<c4047e68-57c9-f5b8-77b9-745f859ac706@PointedEars.de>
In reply to#35937
Thomas 'PointedEars' Lahn wrote:
> Always write and test programs in strict mode unless you have a good reason
> not to:
> 
>     set: function (x) {
>       'use strict';
> 
>       console.log('FactoryA.set :=', x);
> 
>       /* throws ReferenceError exception */
>       index = x;
>     }
> 
> When this part is working, move the 'use strict' to the next outer scope
> until it is at the global scope.

… which is also in the global execution context.

> If you are using ES 6 modules, you can simply keep it there; if not, wrap
> everything (including the strict mode declaration) into
> 
>   (function (global) {
>     // …
>   }(this));
> 
> Keep in mind that you have to use “global” then to refer to the global
> object as you are then in function (local) scope.

In general, you are only in function (local) _execution context_ then.  But
strict mode also changes the scope: unless the function is explicitly called
as a method, the value of “this” is “undefined”, whereas previously it could
have referred to the global object.  Hence the introduction of the formal
parameter, “global”, for which the global value of “this” (the reference to
the global object) is passed.

“Scope” and “execution context” are separate, but related concepts.

-- 
PointedEars

Twitter: @PointedEars2
Please do not cc me. / Bitte keine Kopien per E-Mail.

[toc] | [prev] | [next] | [standalone]


#35958

FromMatt <msg2maciej@gmail.com>
Date2019-01-06 19:15 -0800
Message-ID<b7dc89d5-162a-40d4-b882-116736957751@googlegroups.com>
In reply to#35938
Sorry dude I have no time to process your long response.
Anyway, never mind. I've read more manuals\specs, I do use different design pattern now. And my code works as it suppose.
As for NO pro coder I know basics of C, C++, JavaScript, PHP, SQL, and some other, enough. I started with BASIC on 8bit machine, good few decades ago, in case if you need to know?
Adieu.

[toc] | [prev] | [next] | [standalone]


#35960

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2019-01-11 00:56 +0100
Message-ID<adc2b318-2b4b-fdda-6842-4fa8934c179b@PointedEars.de>
In reply to#35958
Matt wrote:
^^^^
I should have refrained from responding the moment I saw the lack of real name.

> Sorry dude I have no time to process your long response.

So we should invest our previous free time to help you understand and solve
*your* problems, and you can’t even be bothered to read our responses?

FOAD!

*PLONK*

F’up2 poster

-- 
PointedEars

Twitter: @PointedEars2
Please do not cc me. / Bitte keine Kopien per E-Mail.

[toc] | [prev] | [next] | [standalone]


#35939

From"R.Wieser" <address@not.available>
Date2018-12-31 10:07 +0100
Message-ID<q0cm8n$99j$1@gioia.aioe.org>
In reply to#35937
"Thomas 'PointedEars' Lahn" <PointedEars@web.de> wrote in message 
news:b780b412-9227-9f24-e568-057ddc612943@PointedEars.de...
> Matt wrote:
> ^^^^
> Your full real name belongs there.

And that coming from someone who posts with a made-up one ?   And don't tell 
me your parents actually baptized you with "PointedEars" as a middle name. 
:-)

Kiddo, your advice (which could be sound, as you're not really stupid in 
that regard) would have *much* more value and impact when you would not 
poisson it with demands towards your own preferences.

My preference is not to have to deal with arrogancy like yours, but I'm also 
quite confident that you will simply ignore that.  And if thats so, what 
makes you think that you can demand other behaviour towards your own ?    An 
"only my view of {x} is the true one" stance combined with hypocricy perhaps 
?

I let you out of my blacklist, but you're not ready for it yet.  Back in you 
go again.

Regards,
Rudy Wieser

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.javascript


csiph-web