jemand anders
Erfahrenes Mitglied
Hallo,
warum ist im Script unten bei return der Wert "carManager[name] &&" notwendig? Wenn der Wert fehlt, läuft es nicht, aber warum?
Noch etwas, warum kann ich "apply( carManager," durch "apply( x," ersetzen, und es läuft dennoch?
Gruß
warum ist im Script unten bei return der Wert "carManager[name] &&" notwendig? Wenn der Wert fehlt, läuft es nicht, aber warum?
Noch etwas, warum kann ich "apply( carManager," durch "apply( x," ersetzen, und es läuft dennoch?
Gruß
Javascript:
// https://addyosmani.com/resources/essentialjsdesignpatterns/book/#factorypatternjavascript
// test-command-pattern.js
//(function(){
var carManager = {
// request information
requestInfo: function( model, id ){
console.log("Log: The information for " + model + " with ID " + id + " is foobar");
return "Ret: The information for " + model + " with ID " + id + " is foobar";
},
// purchase the car
buyVehicle: function( model, id ){
console.log( "Log: You have successfully purchased Item " + id + ", a " + model);
return "Ret: You have successfully purchased Item " + id + ", a " + model;
},
// arrange a viewing
arrangeViewing: function( model, id ){
console.log("Log: You have successfully booked a viewing of " + model + " ( " + id + " ) ");
return "Ret: You have successfully booked a viewing of " + model + " ( " + id + " ) ";
}
};
//})();
console.clear();
carManager.execute = function ( name ) {
//console.log(typeof name + " name = " + name)
//console.log(carManager[name]);
//console.log(arguments.length);
//console.log(carManager[name])
return carManager[name] && carManager[name].apply( carManager, [].slice.call(arguments, 1) );
};
//console.log('exe')
carManager.execute( "xyz", "VW", "12345" );
//console.log('exe')
carManager.execute( "arrangeViewing", "Ferrari", "14523" );
//console.log('exe')
carManager.execute( "requestInfo", "Ford Mondeo", "54323" );
//console.log('exe')
carManager.execute( "requestInfo", "Ford Escort", "34232" );
//console.log('exe')
carManager.execute( "buyVehicle", "Ford Escort", "34232" );
Zuletzt bearbeitet: