+ 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).
5 Answers
+ 3
make it public in sololearn
+ 3
in js you don't need to create functions to return this.pos . officeP.pos is enough
+ 2
because this.pos
+ 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
+ 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.