Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #35937
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Newsgroups | comp.lang.javascript |
| Subject | Re: factory pattern, seems like object property gets overwritten somehow |
| Date | 2018-12-30 22:30 +0100 |
| Organization | PointedEars Software (PES) |
| Message-ID | <b780b412-9227-9f24-e568-057ddc612943@PointedEars.de> (permalink) |
| References | <511ba0cd-7b28-48c8-821d-166b45a22a62@googlegroups.com> |
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.
Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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