Reputation: 31
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
Upvotes: 3
Views: 340
Reputation: 257
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);
Upvotes: 1
Reputation: 8183
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)
Upvotes: 1
Reputation: 176
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()
Upvotes: 0
Reputation: 18249
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);
Upvotes: 0
Reputation: 7285
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);
Upvotes: 0
Reputation: 36564
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);
Upvotes: 1
Reputation: 363
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
Upvotes: 0
Reputation: 2293
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
Upvotes: 0