Here is the Javascript code I currently have. I need to convert this into a component class within an Angular component.
As far as I understand, the Character.prototype.placeAt()
code is used to add a new method or attribute to an existing object. In this case, the line this.tile = tileTo;
within the placeAt()
function will update an instance of a Character Object with the global variable tileTo
. But how can I translate this into 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 attempted to convert it like this, but I am unable to utilize Character.prototype
in a TypeScript class since it gives the error: 'duplicate identifier Character'. How can I add the placeAt()
method to the Character object?
Is it feasible to access class variables without using this
or passing an instance of the class? Since this
will vary with context, for instance, in the 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(..);
}
}
Just keep in mind, I am relatively new to JavaScript and Angular.