I am migrating from angularjs to angular. I already did a lot, but I am still stuck with factories and prototypes. Card represents a play card.
function Card() {
this.remoteId = 0;
this.version = 0;
this.name = "";
this.stars = 0;
}
Card.prototype = {
getFromRemote: getFromRemote,
initFromRemote: initFromRemote,
updateFromCard: updateFromCard,
createImage: createImage,
};
return (Card);
}
Now I am just wondering hhow to migrate it, because there are no more factories in angular. I was thinking about making the Card function a card interface like in the angular tutorial https://angular.io/tutorial/toh-pt1 . Do you think that is a good idea or do I miss something? Any help or ideas would be appreciated!!!!
You can use a combination of abstract class and constructor:
abstract class
to get functionality of prototype.
Initial value setting logic can go inside the constructor
. playgrond-link
abstract class AbsCard {
abstract remoteId: number;
abstract version: number;
abstract name: string
abstract stars: number;
public initFromRemote(): void {
this.remoteId = 0;
}
public print() {
console.log(this);
}
}
class Card extends AbsCard {
public remoteId: number = 0;
version: number = 0;
name: string = '';
stars: number = 0;
public constructor() {
super();
/* other creation logic can come here */
}
}
let c = new Card();
c.print();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.