函数化1
1 var mammal = function(spec) { 2 var that = {}; 3 4 that.saying = function() { 5 return spec.saying; 6 }; 7 8 that.get_name = function() { 9 return spec.name; 10 }; 11 12 that.says = function() { 13 return this.saying(); 14 }; 15 16 return that; 17 }; 18 19 var myMammal = mammal({ 20 name: 'Herb', 21 saying: 'Hello world!!!' 22 }); 23 24 console.log(myMammal.says());
DC大牛果然对javascript的理解非常透彻,javascript语言精粹真的物超所值,虽然此书很薄。
1 var mammal = function(spec) { 2 var that = {}; 3 4 that.get_name = function() { 5 return spec.name; 6 }; 7 8 that.says = function() { 9 return spec.saying(); 10 }; 11 12 return that; 13 }; 14 15 var myMammal = mammal({ 16 name: 'Herb', 17 saying: function() { 18 return 'Hello world!!!' 19 } 20 }); 21 22 console.log(myMammal.says());