Irgendwelche Ideen oder Vorschläge für eine prägnantere Art, diesen Code zu refaktorieren?Haben Sie gute Vorschläge zum Refactoring des folgenden JavaScript-Codes?
Vielleicht eine Loop-Lösung oder etwas ähnliches?
this._featuredImage = '../../../../../../../../content/images/' + this.post.slug + '.jpg';
this._checkImage(this._featuredImage, function() { // Image exists
this.featuredImage = this._featuredImage;
}.bind(this), function() { // Image doesn't exist
this._featuredImage = '../../../../../../../../content/images/' + this.post.slug + '.png';
this._checkImage(this._featuredImage, function() { // Image exists
this.featuredImage = this._featuredImage;
}.bind(this), function() { // Image doesn't exist
this._featuredImage = '../../../../../../../../content/images/' + this.post.datestamp + '.jpg';
this._checkImage(this._featuredImage, function() { // Image exists
this.featuredImage = this._featuredImage;
}.bind(this), function() { // Image doesn't exist
this._featuredImage = '../../../../../../../../content/images/' + this.post.datestamp + '.png';
this._checkImage(this._featuredImage, function() { // Image exists
this.featuredImage = this._featuredImage;
}.bind(this), function() { // Image doesn't exist
this.featuredImage = false;
}.bind(this));
}.bind(this));
}.bind(this));
}.bind(this));
Danke, ich hoffe, das macht Sinn.
Diese Ordnerpfade sind schrecklich '../../../../../../../../' lol. Stattdessen 'var imgPath = '../../../../../../../../ content/images /'' dann 'this._featuredImage = imgPath + this.post.slug' und so weiter. – Marcus
Dies gehört in den Code Review Stack Exchange. –
Wenn Sie dies auf Code Review veröffentlichen, müssen Sie wirklich erklären, was dieser Code tut, sonst wird es abgelehnt und wahrscheinlich als unklar geschlossen. –