1 <!doctype html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>Document</title> 6 <script src='./js/underscore.js'></script> 7 <script src='./js/backbone.js'></script> 8 9 </head> 10 11 <body> 12 13 14 15 <script type="text/javascript"> 16 17 //建立一個數據模型 18 19 var student = Backbone.Model.extend({ 20 21 //初始化函數 22 initialize:function(){ 23 24 num++; 25 console.log(num); 26 }, 27 28 //設置數據模型的默認值 29 defaults:{ 30 name:"", 31 phone:"" 32 } 33 34 }); 35 36 var num = 0; 37 var s1 = new student(); 38 //模型對象賦值的方法 39 s1.set({name:"黃澤淋",phone:"13751877760"}); 40 41 var s2 = new student(); 42 43 //獲取模型對象的屬性值方法get(attrName)或者escape(attrName) 44 45 console.log(s1.get("phone")); 46 </script> 47 </body> 48 </html>