//做饭 let cook = function(){ let FoodMaterial = 'Tomatoes'; //食材:西红柿 let Spice = '食用盐'; //作料:食用盐 return { buy:function(){ console.log('购买' + FoodMaterial + '和' + 'Spice' ); }, doFood:function(){ console.log(`开始做${FoodMaterial}`); }, setFood:function(food){ //未传参则不执行赋值 food && (FoodMaterial = food); } } }let o = cook(); o.setFood('肉'); o.buy();
let cook = { FoodMaterial : 'Tomatoes', Spice :'食用盐', buy:function(){ console.log('购买' + FoodMaterial + '和' + 'Spice' ); }, doFood:function(){ console.log(`开始做${FoodMaterial}`); }, setFood:function(food){ //未传参则不执行赋值 food && (FoodMaterial = food); } } let o = cook; o.setFood('肉'); o.buy(); o.doFood();
问题一:
以上代码结果一样,但是有什么不同么,用哪个更好?
问题二:
在不用js框架的情况下,请问怎么编写代码比较好,附代码更好
凤凰求蛊
相关分类