Below is the code I have in Javascript. I need to write this in a component class inside an Angular component.
Based on my understanding, Character.prototype.placeAt() code adds a new method or attribute to an existing object. Or, in this case, this.tile = tileTo; inside placeAt() will update an instance of Character Object with global tileTo variable. But how do I convert this to Typescript?
<script type="text/javascript">
tileFrom:any = [1, 1];
tileTo = [1, 1];
function Character() {
this.tileFrom = [1, 1];
this.tileTo = [1, 1];
this.tile = tileTo;
}
Character.prototype.placeAt = function (x, y) {
this.tileFrom = [x, y];
this.tileTo = [x, y];
this.tile = tileTo;
};
var player = new Character();
player.placeAt(..);
</script>
I tried converting it as follows but I cannot use Character.prototype in a typescript class as I get the error: 'duplicate identifier Character'. So how do I add placeAt() to the Character object?
Is it possible to access class variables without using this or sending an instance of the class? Since this will change with context, i.e. in placeAt() method, this refers to the Character object.
export class GametrainComponent implements AfterViewInit {
tileFrom = [1, 1];
tileTo = [1, 1];
Character(self) {
console.log("ss "+self.tileW)
this.tileFrom = [1, 1];
this.tileTo = [1, 1];
this.tile = self.tileTo;
};
Character.prototype.placeAt(x:any, y:any, self) { //error duplicate identifier Character
this.tileFrom = [x, y];
this.tileTo = [x, y];
this.tile = self.tileTo;
};
ngAfterViewInit() {
self = this;
this.player = new this.Character(self);
player.placeAt(..);
}
}
Please note, I am new to JavaScript and Angular.
any.