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>
时间: 2024-12-29 01:28:15