How would I set up a method to update passed properties of JavaScript object?
up vote
0
down vote
favorite
I'm looking to create a method for an object to update only the properties that are passed.
for example
function Car () {
this.color = 'yellow'
this.brand = 'bmw'
this.key = 0
this.update = function (o) {
this.key++
// maybe Object.assign(this, o) somehow ??
}
}
If I do Car.update({color:'red'})
I want to update the object but not overwrite the brand. I want to use something like Object.assign(this, o)
but not sure how to assign it. Do I need to create a prototype? Car.prototype.update = ?
javascript object methods prototype
add a comment |
up vote
0
down vote
favorite
I'm looking to create a method for an object to update only the properties that are passed.
for example
function Car () {
this.color = 'yellow'
this.brand = 'bmw'
this.key = 0
this.update = function (o) {
this.key++
// maybe Object.assign(this, o) somehow ??
}
}
If I do Car.update({color:'red'})
I want to update the object but not overwrite the brand. I want to use something like Object.assign(this, o)
but not sure how to assign it. Do I need to create a prototype? Car.prototype.update = ?
javascript object methods prototype
2
So what went wrong withObject.assign
? It will updatethis
if you pass that as first argument. No need to assign anything more.
– trincot
Nov 11 at 21:08
but i would have to assign it to something right? like this = Object.assign(this, o) but that's a no-go
– Olgo
Nov 11 at 21:24
You need to create anew Car()
first
– charlietfl
Nov 11 at 21:35
@trincot Ah, i see. looking at the docs, i didn't realize it copies to the first parameter which is the target. I thought it had to return something which you would then save to a var. convert that to an answer. :)
– Olgo
Nov 11 at 21:35
OK, I have done so.
– trincot
Nov 11 at 21:42
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I'm looking to create a method for an object to update only the properties that are passed.
for example
function Car () {
this.color = 'yellow'
this.brand = 'bmw'
this.key = 0
this.update = function (o) {
this.key++
// maybe Object.assign(this, o) somehow ??
}
}
If I do Car.update({color:'red'})
I want to update the object but not overwrite the brand. I want to use something like Object.assign(this, o)
but not sure how to assign it. Do I need to create a prototype? Car.prototype.update = ?
javascript object methods prototype
I'm looking to create a method for an object to update only the properties that are passed.
for example
function Car () {
this.color = 'yellow'
this.brand = 'bmw'
this.key = 0
this.update = function (o) {
this.key++
// maybe Object.assign(this, o) somehow ??
}
}
If I do Car.update({color:'red'})
I want to update the object but not overwrite the brand. I want to use something like Object.assign(this, o)
but not sure how to assign it. Do I need to create a prototype? Car.prototype.update = ?
javascript object methods prototype
javascript object methods prototype
edited Nov 11 at 21:26
asked Nov 11 at 21:04
Olgo
134
134
2
So what went wrong withObject.assign
? It will updatethis
if you pass that as first argument. No need to assign anything more.
– trincot
Nov 11 at 21:08
but i would have to assign it to something right? like this = Object.assign(this, o) but that's a no-go
– Olgo
Nov 11 at 21:24
You need to create anew Car()
first
– charlietfl
Nov 11 at 21:35
@trincot Ah, i see. looking at the docs, i didn't realize it copies to the first parameter which is the target. I thought it had to return something which you would then save to a var. convert that to an answer. :)
– Olgo
Nov 11 at 21:35
OK, I have done so.
– trincot
Nov 11 at 21:42
add a comment |
2
So what went wrong withObject.assign
? It will updatethis
if you pass that as first argument. No need to assign anything more.
– trincot
Nov 11 at 21:08
but i would have to assign it to something right? like this = Object.assign(this, o) but that's a no-go
– Olgo
Nov 11 at 21:24
You need to create anew Car()
first
– charlietfl
Nov 11 at 21:35
@trincot Ah, i see. looking at the docs, i didn't realize it copies to the first parameter which is the target. I thought it had to return something which you would then save to a var. convert that to an answer. :)
– Olgo
Nov 11 at 21:35
OK, I have done so.
– trincot
Nov 11 at 21:42
2
2
So what went wrong with
Object.assign
? It will update this
if you pass that as first argument. No need to assign anything more.– trincot
Nov 11 at 21:08
So what went wrong with
Object.assign
? It will update this
if you pass that as first argument. No need to assign anything more.– trincot
Nov 11 at 21:08
but i would have to assign it to something right? like this = Object.assign(this, o) but that's a no-go
– Olgo
Nov 11 at 21:24
but i would have to assign it to something right? like this = Object.assign(this, o) but that's a no-go
– Olgo
Nov 11 at 21:24
You need to create a
new Car()
first– charlietfl
Nov 11 at 21:35
You need to create a
new Car()
first– charlietfl
Nov 11 at 21:35
@trincot Ah, i see. looking at the docs, i didn't realize it copies to the first parameter which is the target. I thought it had to return something which you would then save to a var. convert that to an answer. :)
– Olgo
Nov 11 at 21:35
@trincot Ah, i see. looking at the docs, i didn't realize it copies to the first parameter which is the target. I thought it had to return something which you would then save to a var. convert that to an answer. :)
– Olgo
Nov 11 at 21:35
OK, I have done so.
– trincot
Nov 11 at 21:42
OK, I have done so.
– trincot
Nov 11 at 21:42
add a comment |
3 Answers
3
active
oldest
votes
up vote
0
down vote
accepted
Object.assign
mutates the first argument, so there is no need to assign the result of Object.assign()
to something.
Object.assign(this, o);
...will work fine.
function Car () {
this.color = 'yellow'
this.brand = 'bmw'
this.key = 0
this.update = function (o) {
this.key++
Object.assign(this, o);
}
}
var car = new Car();
car.update({color: 'red'});
console.log(car);
add a comment |
up vote
1
down vote
The idea you suggested in your OP works fine.
function Car () {
this.color = 'yellow';
this.brand = 'bmw';
this.key = 0;
this.update = function (o) {
Object.assign(this, o);
}
}
a = new Car();
a.update({color: "red"});
console.log(a.color + " - " + a.brand + " - " + a.key);
// returns "red - bmw - 0"
add a comment |
up vote
0
down vote
const object1 = {
color : 'red'
};
const obj2 = Object.assign({color: 'blue', d: 5}, object1);
console.log(obj2.color); // red
Object.assign() method -> copy the values of all enumerable own properties.
Finally It will return the target object
I just wanted it to be in a method, to keep it clean and so i can tie other functionality to it whenever there is an update, i updated my question to show what else I would like to tie in. to give a better idea of what i'm trying to accomplish
– Olgo
Nov 11 at 21:23
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
Object.assign
mutates the first argument, so there is no need to assign the result of Object.assign()
to something.
Object.assign(this, o);
...will work fine.
function Car () {
this.color = 'yellow'
this.brand = 'bmw'
this.key = 0
this.update = function (o) {
this.key++
Object.assign(this, o);
}
}
var car = new Car();
car.update({color: 'red'});
console.log(car);
add a comment |
up vote
0
down vote
accepted
Object.assign
mutates the first argument, so there is no need to assign the result of Object.assign()
to something.
Object.assign(this, o);
...will work fine.
function Car () {
this.color = 'yellow'
this.brand = 'bmw'
this.key = 0
this.update = function (o) {
this.key++
Object.assign(this, o);
}
}
var car = new Car();
car.update({color: 'red'});
console.log(car);
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
Object.assign
mutates the first argument, so there is no need to assign the result of Object.assign()
to something.
Object.assign(this, o);
...will work fine.
function Car () {
this.color = 'yellow'
this.brand = 'bmw'
this.key = 0
this.update = function (o) {
this.key++
Object.assign(this, o);
}
}
var car = new Car();
car.update({color: 'red'});
console.log(car);
Object.assign
mutates the first argument, so there is no need to assign the result of Object.assign()
to something.
Object.assign(this, o);
...will work fine.
function Car () {
this.color = 'yellow'
this.brand = 'bmw'
this.key = 0
this.update = function (o) {
this.key++
Object.assign(this, o);
}
}
var car = new Car();
car.update({color: 'red'});
console.log(car);
function Car () {
this.color = 'yellow'
this.brand = 'bmw'
this.key = 0
this.update = function (o) {
this.key++
Object.assign(this, o);
}
}
var car = new Car();
car.update({color: 'red'});
console.log(car);
function Car () {
this.color = 'yellow'
this.brand = 'bmw'
this.key = 0
this.update = function (o) {
this.key++
Object.assign(this, o);
}
}
var car = new Car();
car.update({color: 'red'});
console.log(car);
answered Nov 11 at 21:41
trincot
115k1478109
115k1478109
add a comment |
add a comment |
up vote
1
down vote
The idea you suggested in your OP works fine.
function Car () {
this.color = 'yellow';
this.brand = 'bmw';
this.key = 0;
this.update = function (o) {
Object.assign(this, o);
}
}
a = new Car();
a.update({color: "red"});
console.log(a.color + " - " + a.brand + " - " + a.key);
// returns "red - bmw - 0"
add a comment |
up vote
1
down vote
The idea you suggested in your OP works fine.
function Car () {
this.color = 'yellow';
this.brand = 'bmw';
this.key = 0;
this.update = function (o) {
Object.assign(this, o);
}
}
a = new Car();
a.update({color: "red"});
console.log(a.color + " - " + a.brand + " - " + a.key);
// returns "red - bmw - 0"
add a comment |
up vote
1
down vote
up vote
1
down vote
The idea you suggested in your OP works fine.
function Car () {
this.color = 'yellow';
this.brand = 'bmw';
this.key = 0;
this.update = function (o) {
Object.assign(this, o);
}
}
a = new Car();
a.update({color: "red"});
console.log(a.color + " - " + a.brand + " - " + a.key);
// returns "red - bmw - 0"
The idea you suggested in your OP works fine.
function Car () {
this.color = 'yellow';
this.brand = 'bmw';
this.key = 0;
this.update = function (o) {
Object.assign(this, o);
}
}
a = new Car();
a.update({color: "red"});
console.log(a.color + " - " + a.brand + " - " + a.key);
// returns "red - bmw - 0"
function Car () {
this.color = 'yellow';
this.brand = 'bmw';
this.key = 0;
this.update = function (o) {
Object.assign(this, o);
}
}
a = new Car();
a.update({color: "red"});
console.log(a.color + " - " + a.brand + " - " + a.key);
// returns "red - bmw - 0"
function Car () {
this.color = 'yellow';
this.brand = 'bmw';
this.key = 0;
this.update = function (o) {
Object.assign(this, o);
}
}
a = new Car();
a.update({color: "red"});
console.log(a.color + " - " + a.brand + " - " + a.key);
// returns "red - bmw - 0"
answered Nov 11 at 21:40
M. L.
261
261
add a comment |
add a comment |
up vote
0
down vote
const object1 = {
color : 'red'
};
const obj2 = Object.assign({color: 'blue', d: 5}, object1);
console.log(obj2.color); // red
Object.assign() method -> copy the values of all enumerable own properties.
Finally It will return the target object
I just wanted it to be in a method, to keep it clean and so i can tie other functionality to it whenever there is an update, i updated my question to show what else I would like to tie in. to give a better idea of what i'm trying to accomplish
– Olgo
Nov 11 at 21:23
add a comment |
up vote
0
down vote
const object1 = {
color : 'red'
};
const obj2 = Object.assign({color: 'blue', d: 5}, object1);
console.log(obj2.color); // red
Object.assign() method -> copy the values of all enumerable own properties.
Finally It will return the target object
I just wanted it to be in a method, to keep it clean and so i can tie other functionality to it whenever there is an update, i updated my question to show what else I would like to tie in. to give a better idea of what i'm trying to accomplish
– Olgo
Nov 11 at 21:23
add a comment |
up vote
0
down vote
up vote
0
down vote
const object1 = {
color : 'red'
};
const obj2 = Object.assign({color: 'blue', d: 5}, object1);
console.log(obj2.color); // red
Object.assign() method -> copy the values of all enumerable own properties.
Finally It will return the target object
const object1 = {
color : 'red'
};
const obj2 = Object.assign({color: 'blue', d: 5}, object1);
console.log(obj2.color); // red
Object.assign() method -> copy the values of all enumerable own properties.
Finally It will return the target object
answered Nov 11 at 21:12
Nattamai Jawaharlal Manikandan
2068
2068
I just wanted it to be in a method, to keep it clean and so i can tie other functionality to it whenever there is an update, i updated my question to show what else I would like to tie in. to give a better idea of what i'm trying to accomplish
– Olgo
Nov 11 at 21:23
add a comment |
I just wanted it to be in a method, to keep it clean and so i can tie other functionality to it whenever there is an update, i updated my question to show what else I would like to tie in. to give a better idea of what i'm trying to accomplish
– Olgo
Nov 11 at 21:23
I just wanted it to be in a method, to keep it clean and so i can tie other functionality to it whenever there is an update, i updated my question to show what else I would like to tie in. to give a better idea of what i'm trying to accomplish
– Olgo
Nov 11 at 21:23
I just wanted it to be in a method, to keep it clean and so i can tie other functionality to it whenever there is an update, i updated my question to show what else I would like to tie in. to give a better idea of what i'm trying to accomplish
– Olgo
Nov 11 at 21:23
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53253226%2fhow-would-i-set-up-a-method-to-update-passed-properties-of-javascript-object%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
So what went wrong with
Object.assign
? It will updatethis
if you pass that as first argument. No need to assign anything more.– trincot
Nov 11 at 21:08
but i would have to assign it to something right? like this = Object.assign(this, o) but that's a no-go
– Olgo
Nov 11 at 21:24
You need to create a
new Car()
first– charlietfl
Nov 11 at 21:35
@trincot Ah, i see. looking at the docs, i didn't realize it copies to the first parameter which is the target. I thought it had to return something which you would then save to a var. convert that to an answer. :)
– Olgo
Nov 11 at 21:35
OK, I have done so.
– trincot
Nov 11 at 21:42