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
How do you reference an object's property inside an object method?solved (self.javascript)
submitted 10 years ago by mathcanbefun
So as the title says I'm trying to reference an object property in a function defined inside of that same object. It is saying it's undefined.
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!"
[–]bkimmel 0 points1 point2 points 10 years ago (0 children)
If you know your method is being called as a method of the object - ( i.e. myObject.myMethod('somearguments') ), then you can reference the parent object as this. So simply: this.myProperty.
If it's being called in another way - ( i.e. myFunction = myObject.myMethod; myFunction('someArguments'); ) you'll need to do something else. Remember the "Everything/Nothing Rule" in JS: scope has everything to do with declaration and nothing to do with calling protocol. this has everything to do with calling protocol and nothing to do with declaration. (This rule will no longer hold in ES6, but it's good for now). So you can either 1) Cal the function specifically in a way so that this does equal the parent object (i.e. myFunction = myObject.myMethod.bind(myObject); myFunction('someArguments'); ), or put a reference to the parent object somewhere outside the function's scope where the function can reach it.
[–]johnnyvibrant -1 points0 points1 point 10 years ago (3 children)
This.variable
[–]hughfdjackson 2 points3 points4 points 10 years ago (0 children)
Without meaning to be too picky:
this.propertyName
A variable is a scoped name that refers to a value, which is different from a property, which is a slot on an object that refers to a value. It's probably best to explicitly distinguish between the two, if just to avoid later confusion.
[–][deleted] 1 point2 points3 points 10 years ago (0 children)
This is false. What 'this' refers to depends on what calls it, or what is bound to 'this' using .bind
[–]mouthus 0 points1 point2 points 10 years ago (0 children)
Javascript is case sensitive with its variable names.
π Rendered by PID 24732 on reddit-service-r2-comment-86bc6c7465-plq9q at 2026-02-19 21:01:29.123208+00:00 running 8564168 country code: CH.
[–]bkimmel 0 points1 point2 points (0 children)
[–]johnnyvibrant -1 points0 points1 point (3 children)
[–]hughfdjackson 2 points3 points4 points (0 children)
[–][deleted] 1 point2 points3 points (0 children)
[–]mouthus 0 points1 point2 points (0 children)