+ 1

Code has error that does not exist

Im making a code but every time I run it there is an error. I check the code for Xs or warnings, but they cease to exist. Here is the code: - var officeP = { pos: -60, changePos: function(x) { pos += x } } var office = document.getElementById('office') function moveOffice() { if (window.event.clientX <= 122) { officeP.changePos(-3) office.style.left = officeP.pos + "%" } } window.addEventListener('mousemove', moveOffice) //left: 122, right: 381 - The error I get when I move the cursor to the left side is this: " Uncaught ReferenceError: pos is not defined Line: 4 " But I defined "pos" in the variable "officeP" (stands for office properties).

25th Mar 2017, 3:27 PM
koder
koder  - avatar
5 odpowiedzi
+ 3
make it public in sololearn
25th Mar 2017, 3:35 PM
Yaroslav Pieskov
Yaroslav Pieskov - avatar
+ 3
in js you don't need to create functions to return this.pos . officeP.pos is enough
25th Mar 2017, 3:38 PM
Yaroslav Pieskov
Yaroslav Pieskov - avatar
+ 2
because this.pos
25th Mar 2017, 3:29 PM
Yaroslav Pieskov
Yaroslav Pieskov - avatar
+ 1
i put a function in the object. this is the syntax: - , getPos: function() { return this.pos } - then I replaced this: - office.style.left = officeP.pos + "%" - to this - office.style.left = officeP.getPos() + "%" - and i still got the same error
25th Mar 2017, 3:33 PM
koder
koder  - avatar
+ 1
var officeP = { pos: -60, changePos: function(x) { this.pos += x } } Change pos to this.pos and it should work. This is because you are accessing pos as a variable which is not defined, whereas pos is defined as the property of officeP object. So can only be accessed as a property.
25th Mar 2017, 5:26 PM
Ashwani Kumar
Ashwani Kumar - avatar