experchange > javascript

Matt (12-30-18, 08:35 PM)
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>
Thomas 'PointedEars' Lahn (12-30-18, 11:30 PM)
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?
Thomas 'PointedEars' Lahn (12-30-18, 11:46 PM)
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.
R.Wieser (12-31-18, 11:07 AM)
"Thomas 'PointedEars' Lahn" <PointedEars> wrote in message
news:2943
> 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
Matt (01-07-19, 05:15 AM)
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.
Thomas 'PointedEars' Lahn (01-11-19, 01:56 AM)
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
Similar Threads