3

So in my code I have an object:

function myObject(value1, value2, value3){
    this.value1 = value1;
    this.value2 = value2;
    this.value3 = value3;
}

Then I have a function:

function myFunction(value1, value2, value3) {
    value1 += value2;
    value3 += 1;
};

How could I use something like this to change the value of the object. For example:

var object1 = new myObject(1,2,3);

So eventually, value1 would become 3, and value3 would become 4. Thank you in advance, I'm new to OOP

Charis Moutafidis
  • 363
  • 1
  • 4
  • 17
person341
  • 31
  • 2
  • I'm using the object for other things, I just want a function I can call that changes some of the values of the object, and other variables. – person341 May 09 '19 at 14:12

8 Answers8

1

You can pass a reference of your object to myFunction and there you can change the values.

function myObject(value1, value2, value3) {
  this.value1 = value1;
  this.value2 = value2;
  this.value3 = value3;
}

function myFunction(obj) {
  obj.value1 += obj.value2;
  obj.value3 += 1;
};

var object1 = new myObject(1, 2, 3);
myFunction(object1);
console.log(object1)
Vladu Ionut
  • 8,075
  • 1
  • 19
  • 30
  • I didin't think about that sorry! Thank you. – person341 May 09 '19 at 14:17
  • @VladuIonut Answers without a single word of explanation are not good. Try adding some explanation. – Maheer Ali May 09 '19 at 14:26
  • 2
    Just passed by to upvote so balance is restored :P I agree with Maheer Ali but this example is an ultra simple solution, no comments needed. Also man, no need to be a cunt, you can just comment and request for more explanations that downvoting. Reputation does not grow in trees you know. – Charis Moutafidis May 09 '19 at 14:29
1

You can pass arguments to that function by spreading them. And return an object from that function and then merge it with this

function myObject(...values){
    Object.assign(this,myFunction(...values))
}
function myFunction(value1, value2, value3) {
    value1 += value2;
    value3 += 1;
    return {value1,value2,value3};
};

const x = new myObject(1,2,3);
console.log(x);
Maheer Ali
  • 35,834
  • 5
  • 42
  • 73
1
function myObject(value1, value2, value3){
    var obj = {};
    obj.value1 = value1;
    obj.value2 = value2;
    obj.value3 = value3;
    return obj;
    }

function myFunction() {
    object1.value1 += object1.value2;
    object1.value3 += 1;
};

var object1 = new myObject(1,2,3);
myFunction(object1);

console.log(object1);
Lioness100
  • 8,260
  • 6
  • 18
  • 49
iliasse
  • 247
  • 1
  • 7
0

If you want to add a method to MyClass you can do:

function MyClass(value1, value2, value3) {
  if (!(this instanceof MyClass)) throw Error("Should be called using new");
  this.value1 = value1;
  this.value2 = value2;
  this.value3 = value3;
}

MyClass.prototype.myFunction = function() {
  this.value1 += this.value2;
  this.value3 += 1;
};


var object1 = new MyClass(1, 2, 3);
console.log(object1);
object1.myFunction();
console.log(object1);

If you just want a function that changes an instance of MyClass you can do:

function MyClass(value1, value2, value3) {
  if (!(this instanceof MyClass)) throw Error("Should be called using new");
  this.value1 = value1;
  this.value2 = value2;
  this.value3 = value3;
}

function myFunction(myClassInstance) {
  myClassInstance.value1 += myClassInstance.value2;
  myClassInstance.value3 += 1;
};


var object1 = new MyClass(1, 2, 3);
console.log(object1);
myFunction(object1);
console.log(object1);

If you want to create a new function that has the instance bound to it you can do:

function MyClass(value1, value2, value3) {
  if (!(this instanceof MyClass)) throw Error("Should be called using new");
  this.value1 = value1;
  this.value2 = value2;
  this.value3 = value3;
}

function getMyFunction(myClassInstance) {
  return function() {
    myClassInstance.value1 += myClassInstance.value2;
    myClassInstance.value3 += 1;
  };
};


var object1 = new MyClass(1, 2, 3);
var myFunction = getMyFunction(object1);
console.log(object1);
myFunction();
console.log(object1);
nick zoum
  • 7,216
  • 7
  • 36
  • 80
0

This is what you're looking for?

 class myObject {
    
        constructor(value1, value2, value3)
        {
            this.value1 = value1;
            this.value2 = value2;
            this.value3 = value3;
        }
    
        myFunction() {
            this.value1 += this.value2;
            this.value3 += 1;
        }
    }
    
    var object1 = new myObject(1,2,3);
    
    object1.myFunction();
    
    console.log(object1.value1); //3
    console.log(object1.value3); //4
chatnoir
  • 2,185
  • 1
  • 15
  • 17
0

I would say the best way to do this would be to add this as a method to the objects you create:

function myObject(value1, value2, value3) {
    this.value1 = value1;
    this.value2 = value2;
    this.value3 = value3;
}

myObject.prototype.myMethod = function() {
    this.value1 += this.value2;
    this.value3 += 1;
};

var object1 = new myObject(1,2,3);
object1.myMethod();

console.log(object1);

Alternatively, with ES6, you can do the same think more naturally by using a class:

class myObject {
    constructor(value1, value2, value3) {
        this.value1 = value1;
        this.value2 = value2;
        this.value3 = value3;
    }

    myMethod() {
        this.value1 += this.value2;
        this.value3 += 1;
    }
}

var object1 = new myObject(1,2,3);
object1.myMethod();

console.log(object1);
Robin Zigmond
  • 17,805
  • 2
  • 23
  • 34
0

One option is to change the definition of your constructor.

function myObject(value1, value2, value3){
    this.value1 = value1 + value2;
    this.value2 = value2;
    this.value3 = value3 = 1;
}

Another option is to pass the custom object as a parameter to the function.

function myFunction(object){
  object.value1 += object.value2;
  object.value3 += 1;
}

function myObject(value1, value2, value3){
  this.value1 = value1;
  this.value2 = value2;
  this.value3 = value3;
}

var object = new myObject(1, 2, 3);

console.log(object.value1);

myFunction(object);

console.log(object.value1);

The output of this execution would be:

1
3
Charis Moutafidis
  • 363
  • 1
  • 4
  • 17
0

Generally, you don't want to change the values you used to initialize the object. What you want are accessor methods to access the data whatever way you wish to present the data. Here is an example of doing something similar to what you are asking.

function Person(first, last) {
    this.firstName = first;
    this.lastName = last;
    this.name = function() {
       return this.firstName + " " + this.lastName;
    };
}

So using this example, you would create a new person object

var object = new Person('First', 'Last')

Then calling the name method would return the data formatted in whatever way you wish to present the data.

var name = object.name()
Doug Moses
  • 176
  • 1
  • 6