Please Help With This Javascript Code - Programming - Nairaland
Nairaland Forum › Science/Technology › Programming › Please Help With This Javascript Code (984 Views)
| Please Help With This Javascript Code by arabmoney14(op): 7:46pm On Jan 01, 2022 |
I am following a JavaScript tutorial in which the tutor runs a similar code as below. It's supposed to create a new property called age and set the value to (2022 - birth year). Apparently, mine outputs undefined. Anyone with an idea why?
|
| Re: Please Help With This Javascript Code by TinubuFirstBorn: 8:25pm On Jan 01, 2022 |
const profile = { name: "Michael", level: 100, birthYear: 2003, calcAge() { let age = 2022 - this.birthYear; return age; }, }; console.log(profile.calcAge()); |
| Re: Please Help With This Javascript Code by TinubuFirstBorn: 8:31pm On Jan 01, 2022 |
const profile = { name: "Michael", level: 100, birthYear: 2003, calcAge() { this.age = 2022 - this.birthYear; return this.age; }, }; console.log(profile.calcAge()); |
| Re: Please Help With This Javascript Code by TinubuFirstBorn: 8:36pm On Jan 01, 2022 |
The two ways I posted will output the value of age. You have to return the age from the function. You also have to call the function: profile.calcAge() |
| Re: Please Help With This Javascript Code by jbreezy: 8:43pm On Jan 01, 2022 |
@ op...Do you know about Scoping in Javascript? You can't access a variable located in a local scope outside its curly parenthesis. |
| Re: Please Help With This Javascript Code by efelico: 8:55pm On Jan 01, 2022 |
TinubuFirstBorn:@ OP this is what you need. In addition to that you are getting undefine because age is a local variable. Variables declared within a JavaScript function, become LOCAL to the function. They can only be accessed from within the function. Read more on local and global scope |
| Re: Please Help With This Javascript Code by qtguru(m): 10:24pm On Jan 01, 2022 |
this.age is not defined because it's defined in the function, when the function is called, age will be added to the property of the object. Hope that explains |
| Re: Please Help With This Javascript Code by arabmoney14(op): 11:49am On Jan 02, 2022 |
Thanks to everybody TinubuFirstBorn jbreezy efelico qtguru. The code now works as expected after implementing your suggestions |
| Re: Please Help With This Javascript Code by stanliwise(m): 12:44pm On Jan 02, 2022 |
arabmoney14:You need to do this before the console.log() profile.calcAge(); |
| Re: Please Help With This Javascript Code by Xinjin: 2:37pm On Jan 02, 2022*. Modified: 3:05pm On Jan 02, 2022 |
you can do it as @TinubufirstBorn did. see below |
| Re: Please Help With This Javascript Code by Xinjin: 2:48pm On Jan 02, 2022 |
The "this" keyword belongs to the Profile Object you define not the function. and Profile Object doesn't have any property call "age" |
| Re: Please Help With This Javascript Code by duyilekan98(m): 11:27pm On Jan 16, 2022 |
arabmoney14:First of all, there’s no object property called age, so what is this.age referencing? In my example I create an object profile with some properties. I set age to 0, so I can calculate based on current year which I would be passing as a parameter in the calcAge function or method(a method is an object property that has a function). Initially when I check the value of property, age is set to 0. But when I run profile.calcAge(2022), you can see the age changed to 24.
|
| Re: Please Help With This Javascript Code by Naijatask: 8:40am On Jan 17, 2022 |
Apparently, what happened was that you did not call the function which was returning the age variable. You have to call the profile.calcAge() function before it can return the age variable. |
How Can I Run Javascript Code On Vs Code • Help Me With This Javascript Problem • Need Someone To Help With My Javascript Code • 2 • 3 • 4
The Leading Tech Hub Center In Umuahia • Starting Up A Tech Skill Based Academy- ADVISE • We Built An App That Lets You Send Money Via Bluetooth — Even Without Network