1

Hi I'm writing a module in NodeJS in a OOP style.

I have multiples simples objects that contains primitive data and multiple complex objects that contain other objects.

const Simple = function Simple() {
    this.x = 0;
    this.y = 0;
}

Simple.prototype.getArea = function() {
    return this.x * this.y;
}


const Complex = function Complex() {
    this.ownProp = 0;
    this.nestedProp = new Simple();
    this.otherNestedProp = new otherSimple();
}


Complex.prototype.set = function(key, value) {
    this[key] = value;
}

Complex.prototype.otherSet = function(value) {
    Object.assign(this, value);
}

My problem is that users who will use my API can break things by doing this:

let simple = new Simple();
simple.getArea(); // 0

let complex = new Complex();
complex.nestedProp.getArea(); // 0
complex.set('nestedProp', {x: 5, y: 6});
complex.nestedProp.getArea(); // THROW  <----

let complex = new Complex();
complex.nestedProp.getArea(); // 0
complex.set({nestedProp: {x: 5, y: 6});
complex.nestedProp.getArea(); // THROW  <----

Is there a lodash function to only assign values of such nested Object.
Or is there a good way to manage this kind of problems?

Note: I could check for instanceof but I have a lot of modules, and I don't want to manage each specific case.

6
  • what you are trying with otherSet function? Commented Jul 14, 2017 at 18:07
  • Are you ready to migrate your project to use TypeScript? That will solve your problem of allowing only specific type of object assignment. Commented Jul 14, 2017 at 18:11
  • what you want exactly, people will be allowed to do complex.set('nestedProp', {x: 5, y: 6}); or not? even complex.nestedProp = {x: 10, y: 90} this or not? Commented Jul 14, 2017 at 18:12
  • You should try typescript.for oops like implementation. And for this problem you can function that only set x or y value and if inner object is immutable so create function that two parameters and create new inner object. Commented Jul 14, 2017 at 18:19
  • @KoushikChatterjee It's an alternative method to directly set passing a plain object. I don't want people to be allowed to do complex.nested = ... (I would like to throw if they do so). I would probably use Proxy object Commented Jul 14, 2017 at 20:29

1 Answer 1

1

It seems you think passing something like {x: 1, y:2} to Complex.set will magically make x and y end inside of Simple. I think you are confused about how Javascript works, no offense meant.

Here's an implementation that would make things work roughly the way you seem to want.

const Simple = function Simple() {
    this.x = 0;
    this.y = 0;
}

Simple.prototype.getArea = function() {
    return this.x * this.y;
}

Simple.prototype.set = function (x, y) {
  this.x = x;
  this.y = y;
}


const Complex = function Complex() {
    this.nestedProp = new Simple();
}


Complex.prototype.set = function(props) {
    this.nestedProp.set(props.x, props.y);
}

let complex = new Complex();
complex.nestedProp.getArea(); // 0
complex.set({x: 5, y: 6});
complex.nestedProp.getArea(); // 30

The properties x and y are passed explicitly from Complex to Simple until they end where they should. You can either pass x and y as separate parameters (see Simple's set) or as properties of an object (see Complex's set).

But if you thought x and y would make it all the way to the end by themselves you need to study basic OOP before writing code; again, no offense meant.

Sign up to request clarification or add additional context in comments.

3 Comments

No offense at all, but I know how Javascript works. I just dont know about OOP best practices in Javascript since you've so much freedom. Ofc the example I give you is pretty simple, but my code is much more complex, and I don't want to have too much specific cases to handle in each setter.
My apologies then, I hope my answer was of some help!
^^ I had the same idea at start, but it's so much boilerplate and maintenance when you have 100+ objects depending on each other. But thanks bro ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.