這是Ruby書中實用面向對象設計的一個例子。我有興趣將這個ruby代碼翻譯成JavaScript,以便更好地理解JS中的duck-typing。任何人都可以幫助翻譯這段代碼來澄清如何最好的寫在Javascript中,或者至少幫助我開始?鴨子打字:使用鴨子打字將Ruby代碼翻譯成Javascript
此處的旅行類充當其他類(Mechanic,Trip_Coordinator,Driver)的接口。 Trip類中的準備方法使用Duck類型準備工具。
class Trip
attr_reader :bicycles, :customers, :vehicle
def prepare(preparers)
preparers.each {|preparer|
preparer.prepare_trip(self)}
end
end
class Mechanic
def prepare_trip(trip)
# Does something with trip.bicycles
end
end
class Trip_Coordinator
def prepare_trip(trip)
# Does something with trip.customers
end
end
class Driver
def prepare_trip(trip)
# Does something with trip.vehicle
end
end
更新:
我加入的代碼,我認爲這是上面的Ruby代碼的可能翻譯。然而,當我運行代碼我得到以下輸出和錯誤:
mountain
2
jeep
/home/ubuntu/user/tests/trip.js:3
preparer.prepare_trip(this)
TypeError: Object [object Object] has no method 'prepare_trip'
意見或建議,將不勝感激,以進一步提高JS鴨打字的理解。
JavaScript代碼:
var Trip = function(preparers){
return preparers.forEach(function(preparer){
preparer.prepare_trip(this)
}
)};
var Mechanic = function(trip){
// does something with trip.bicycles
prepare_trip: console.log(trip.bicycles);
};
var TripCoordinator = function(trip){
//does something with trip.customers
prepare_trip: console.log(trip.customers);
};
var Driver = function(trip){
//does something with trip.vehicle
prepare_trip: console.log(trip.vehicle);
};
// customer wants to go on a trip for two and needs a car
var planA = {
bicycles: "mountain",
customers: 2,
vehicle: "jeep"
};
//customer wants to go a trip for one and only ride a bike
var planB = {
bicycles: "road",
customers: 1
};
Trip([new Mechanic(planA), new TripCoordinator(planA), new Driver(planA)]);
Trip([new Mechanic(planB), new TripCoordinator(planB)]);
更新2
每從下面FGB的解決方案和建議,我有地方我下面的問題,最終的解決方案。我添加了一個代理來刪除調用者的依賴關係,他們必須知道在創建旅行計劃時他們需要什麼準備。
var Agent = function(plan){
if("bicycles" && "customers" && "vehicle" in plan){
Trip([new Mechanic(plan), new TripCoordinator(plan), new Driver(plan)]);
}
else if(!("vehicle" in plan) && "bicycles" && "customers" in plan){ //A driver is not needed
Trip([new Mechanic(plan), new TripCoordinator(plan)]);
}
};
var Trip = function(preparers){
return preparers.forEach(function(preparer){
preparer.prepare_trip(this)
}
)};
var Mechanic = function(trip){
// does something with trip.bicycles
this.prepare_trip = function() {
console.log(trip.bicycles);
}
};
var TripCoordinator = function(trip){
//does something with trip.customers
this.prepare_trip = function() {
console.log(trip.customers);
}
};
var Driver = function(trip){
//does something with trip.vehicle
this.prepare_trip = function() {
console.log(trip.vehicle);
}
};
// customer wants to go on a trip for two and needs a car
var planA = {
bicycles: "mountain",
customers: 2,
vehicle: "jeep"
};
//customer wants to go a trip for one and only ride a bike
var planB = {
bicycles: "road",
customers: 1
};
Agent(planB);
相關SO討論Example of Javascript Duck Typing?
您可以使用Opal編譯器自動將其翻譯爲JavaScript:http://www.sitepoint.com/opal-ruby-browser-basics/ – 2014-10-04 01:42:14
@AndersonGreen:代碼生成器如何幫助他們理解鴨子打字在JavaScript中? – 2014-10-04 02:26:34
@ muistooshort如果這是一個學習練習,那麼最好手動翻譯它,當然。 – 2014-10-04 02:30:06