把实现同一件事情的相同的代码放到一个函数中,以后如果再想实现这个功能,不需要从新的编写这些代码了,提高代码的重用性
function carFactory(brand,type){ var car={}; car.brand = brand; car.type = type; car.run = function(){ console.log("the car is running "+brand+" "+type); } return car; } var car1 = carFactory("大众","高尔夫"); car1.run();