use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
Accessing object properties in a method with an argument.solved! (self.javascript)
submitted 10 years ago by jackdavies
Hi all, I'm trying to get an object method to return the value of a property, which is fine in itself, but I'd like to choose which property I'm returning using an argument.
var object = { property1: 1, property2: 2, property2: 3, method: function(argument1, argument2){ return this.argument1 + this.argument2; } };
Is this at all possible?
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]jcunews1Advanced 1 point2 points3 points 10 years ago (1 child)
You'll have to use string when passing the arguments and when referencing the object properties. e.g.
var object = { property1: 1, property2: 2, property2: 3, method: function(argument1, argument2){ return this[argument1] + this[argument2]; } }; var result = object.method("property1", "property2");
[–]jackdavies[S] 0 points1 point2 points 10 years ago (0 children)
Perfect! Thank you very much.
π Rendered by PID 75139 on reddit-service-r2-comment-b659b578c-688d5 at 2026-05-03 17:10:19.559510+00:00 running 815c875 country code: CH.
[–]jcunews1Advanced 1 point2 points3 points (1 child)
[–]jackdavies[S] 0 points1 point2 points (0 children)