0

I've written the following as an implementation of the module pattern:

let myClass = function (publicName, privateName)
{
  this.publicVar = publicName;
  let privateVar = privateName;

  this.publicMethod = function ()
  {
    return this.publicVar;
  }
  this.getPublic = function ()
  {
    return this.publicMethod();
  }
  this.setPublic = function (newPublicName)
  {
    this.publicVar = newPublicName;
  }
  this.getPrivate = function ()
  {
    return privateVar;
  }
  this.setPrivate = function (newPrivateName)
  {
    privateMethod(newPrivateName);
  }
  let privateMethod = function (newPrivateName)
  {
    privateVar = newPrivateName;
  }

  return this;
}

let a = new myClass('public A', 'private A');
let b = new myClass('public B', 'private B');

// A
console.log(a.publicVar);
console.log(a.publicMethod());

console.log(a.getPrivate());
a.setPrivate('private A-2');
console.log(a.getPrivate());

console.log(a.getPublic());
a.setPublic('public A-2');
console.log(a.getPublic());

// B
console.log(b.publicVar);
console.log(b.publicMethod());

console.log(b.getPrivate());
b.setPrivate('private B-2');
console.log(b.getPrivate());

console.log(b.getPublic());
b.setPublic('public B-2');
console.log(b.getPublic());

It differs from examples I can generally find on the web though, as it doesn't use an IIFE, and doesn't use a constructor as such...

Is there a problem with what I've done?

I can't see anything wrong with it personally... It seems to encapsulate the methods and variables as it should.

NOTE: I also realise that a lot of the methods are unnecessary, I just wanted to test a few things to see if I could break it.

Jack_Hu
  • 857
  • 6
  • 17
  • "and doesn't use a constructor as such..." what do you mean? You have basically declared a constructor. If you want to consider each `myClass` instance to be a "module" then you are good to go. – Yury Tarabanko Aug 07 '20 at 12:59
  • 1
    BTW `return this` is redundant. – Yury Tarabanko Aug 07 '20 at 13:02
  • I just mean that other examples I've seen call something like `function myClass(a, b) { // Constructor-y stuff }` inside the `myClass` function/object itself... And I know about `return this`, it just helps me remember. I don't use JS very often. – Jack_Hu Aug 07 '20 at 13:03
  • It is not wrong. But the principle of instance method should be ```a.getPrivate===b.getPrivate``` and ```a.getPublic===b. getPublic```. What you wrote gets different result. – dunhuang Aug 07 '20 at 15:05
  • @dunhuang - I guess so... Why is that? Is JavaScript's implementation of the `new` key word not robust enough? – Jack_Hu Aug 07 '20 at 16:53
  • @dunhuang Also, why would this be an issue? Can it be avoided? --- This is exactly the kind of answer I was looking for, if you can respond to this comment with your thoughts, I'd be happy to mark it as solved. – Jack_Hu Aug 07 '20 at 16:59
  • @dunhuang - Having just read through: http://ecma-international.org/ecma-262/5.1/#sec-11.9.6 - It seems that it returns false because they're different objects? `7. Return true if x and y refer to the same object. Otherwise, return false.`... Although I'm not sure it should really be looking at the object, as opposed to the function itself. Ugh... So confused. – Jack_Hu Aug 07 '20 at 17:26
  • if you want to produce instance with 'new' keyword, you should use 'prototype' to define methods. Each object has '__proto__' property which is equal to it's constructor's prototype. ```a.__proto__===b.__proto__; a.__proto__===myClass.prototype```, see https://stackoverflow.com/questions/9959727/proto-vs-prototype-in-javascript – dunhuang Aug 08 '20 at 03:15

0 Answers0