0

Situation:

I have multiple JS-Objects with the same structure.

In all of those Objects there's a property called console.

I'd like to assign a value (the same value) to each of these properties.

Please find below the structure of my code:

NameOfMyObject = function() {

     return {

        actions: null,

        console: null,

        init: function() {}, 

        //object specific functions

    }
};

My Problem:

Currently I'm assigning the value manual, but I want do do it generic.

Code snippet

this.console = console;
this.actions.console = console;
this.fixtures.console = console;

How can I access the properties of my objects?

I hope I asked my question clear enough.

13
  • 1
    this.console = this.actions.console = this.fixtures.console = console Commented Sep 10, 2018 at 7:50
  • Could you provide working code? As it stands now, this.actions.console = console; will give an exception. Commented Sep 10, 2018 at 7:51
  • can you elaborate on what you're trying to do? Commented Sep 10, 2018 at 7:53
  • This code is working. init is like a cunstructor. Commented Sep 10, 2018 at 7:55
  • @Chris Li I'd like to do something like this object.property.console = console; Commented Sep 10, 2018 at 7:55

1 Answer 1

1

Here is how you would share a property across objects:

function MyClass() {};

MyClass.prototype.console = {}; // Define a "common" property on the prototype

var obj1 = new MyClass(); // Create two instances
var obj2 = new MyClass();

Object.getPrototypeOf(obj1).console.id = 13; // Assign a value once...
console.log(obj1.console.id); // ... and it exists on both instances
console.log(obj2.console.id);

The shared property is on the prototype object.

Instead of Object.getPrototypeOf(obj1) you can of course use MyClass.prototype, since you know that obj1 was created by MyClass. They give the same prototype object.

If your property always has an object as value and you don't need to replace that value, only mutate it by setting properties on that object, then you don't need to explicitly reference the prototype to set a new value.

So this works:

function MyClass() {};

MyClass.prototype.console = {}; // Define a "common" property on the prototype

var obj1 = new MyClass(); // Create two instances
var obj2 = new MyClass();

obj1.console.id = 13; // Assign a value once... (getting console from the prototype)
console.log(obj1.console.id); // ... and it exists on both instances
console.log(obj2.console.id);

But if you change console itself, you'll be setting it on the instance, not on the prototype:

function MyClass() {};

MyClass.prototype.console = {}; // Define a "common" property on the prototype

var obj1 = new MyClass(); // Create two instances
var obj2 = new MyClass();

obj1.console = { id: 13}; // Setting an instance property now
console.log(obj1.console.id); // ... and it does not exist on both instances
console.log(obj2.console.id); // == undefined

So if that kind of assignment should still work on the prototype, you need to use the first code block with Object.getPrototypeOf or MyClass.prototype.

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

Comments

Your Answer

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