Problem with Object: Section 12, Getters and Setters 1?


#1

Task: Start by adding an empty .age() setter method
image
Error:/home/ccuser/workspace/learn-javascript-objects-getters-setters/main.js:7
set .age(newAge) {
^
SyntaxError: Unexpected token {
at createScript (vm.js:53:10)
at Object.runInThisContext (vm.js:95:10)
at Module._compile (module.js:543:28)
at Object.Module._extensions…js (module.js:580:10)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3)
at Module.runMain (module.js:605:10)
at run (bootstrap_node.js:427:7)
at startup (bootstrap_node.js:151:9)
Really dont know what Im doing wrong here?
Whole Code:
let person = {
_name: ‘Lu Xun’,
_age: 137

}
set .age(newAge) {

}

Thanks in Advance!


#2

why set .age, why the full stop (.)? The full stop is part of JS syntax, and it doesn’t belong where you put it


#3


#4

i should pay more attention, the getter method should be part of the object


#5

The Result: let person = {
_name: ‘Lu Xun’,
_age: 137

}
set age(newAge) {

}




/home/ccuser/workspace/learn-javascript-objects-getters-setters/main.js:7
set age(newAge) {
^^^
SyntaxError: Unexpected identifier
at createScript (vm.js:53:10)
at Object.runInThisContext (vm.js:95:10)
at Module._compile (module.js:543:28)
at Object.Module._extensions…js (module.js:580:10)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3)
at Module.runMain (module.js:605:10)
at run (bootstrap_node.js:427:7)
at startup (bootstrap_node.js:151:9)


#6

getter methods have to be part of objects or classes, they can’t exist outside of object or class


#7

New Error sorry: image
/home/ccuser/workspace/learn-javascript-objects-getters-setters/main.js:4
set age(newAge) {
^^^
SyntaxError: Unexpected identifier
at createScript (vm.js:53:10)
at Object.runInThisContext (vm.js:95:10)
at Module._compile (module.js:543:28)
at Object.Module._extensions…js (module.js:580:10)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3)
at Module.runMain (module.js:605:10)
at run (bootstrap_node.js:427:7)
at startup (bootstrap_node.js:151:9)


#8

same problem? both getters and setters need to be part of an object (or class), they can’t exist outside the object


#9
  1. Properties and methods need to be separated by a comma. (place , after 137)
  2. Remove everything below line 8. As was mentioned, methods and properties must be placed inside the object.

#10

Got it thanks :smiley:


#11


Sorry I know this is seperate but doesnt make sense to me?


#12

you need to log the persons age to console using console.log()


#13

This topic was automatically closed after 7 days. New replies are no longer allowed.