I'm not very familiar with javascript's grammar, but I want to define a class, but the keyword this not behave as I want. And I want to know the reason, thanks! The javascript code is below:
var GameUI = function(){
this.ctx = this.ctx || null;
this.canvas = document.getElementById('canvas');
this.chessToMove = {x : -1, y :-1};
this.style = 'stype_1';
//this.drawBeginX = 4;
//this.drawBeginY = 17;
this.boardStyle = {
stype_1 : {
beginX : 4,
beginY : 17,
intervalX : 35,
intervalY : 36,
},
stype_2 : {
beginX : 4,
beginY : 17,
intervalX : 10,
intervalY : 10,
},
};
this.init = function(){
if(this.canvas && this.canvas.getContext('2d')){
this.ctx = this.canvas.getContext('2d');
this.loadImgs();
//set listeners
this.canvas.addEventListener('click', this.canvasClick, false);
return true;
}
return false;
};
this.setStyle= function(style){
this.style = style;
};
this.canvasClick = function(e){
//
let x = e.clientX - this.offsetLeft, y = e.clientY - this.offsetTop;
//alert('Canvas is clicked! X: ' + x + ",Y: " + y);
//why this is a canvas obj not a GameUI obj
console.log(/*'in canvasClick: ' + */this);
};
};
Html code:
<html>
<head>
<title> js test </title>
<script type="text/javascript" src="game.js"></script>
<script type="text/javascript" src="ui.js"></script>
<style type="text/css">
.main{
margin:0 auto;
width: 80%;
}
#canvas{
border:1px solid black;
}
</style>
</head>
<body>
<div class='main'>
<canvas id='canvas' width='800' height='600'> Your browser is not supported for canvas element </canvas>
</div>
<button> new game </button>
</body>
</html>
As you see, I defined a GameUI class which a canvas was in it with a click function named canvasClick.But surprisely, in the click function, keyword this
is a canvas object not a GameUI object, I test it by a log.And this confused me!