js中的对象
创建
var obj = {name: 'kobe', age: 39}
可以将属性定义为方法
var util = {add:function(args){..}, substact: function() { ... }}
调用
util.add(args)
;
使用方式,例子(类似于java中的类,有实例域和方法,ES6中有JS类的概念,值得研究)
var DataPage = { tableId: null, url: null, page: 1, htmStr: '', rows: 10, searchData: {}, init: function (tableId, url, htmStr, searchData, rows) { this.tableId = tableId; this.url = url; this.htmStr = htmStr; if (!isNaN(rows) && rows > 0) this.rows = rows; this.page(this.loadData(tableId, url, 1, htmStr, searchData)) return this; }, page: function(...){...} }
jQuery中的$.extend方法
1,合并多个对象
$.extend{obj, util}
2,深度嵌套对象
$.extend({ name: “John”, location: { city: “Boston” } }, { last: “Resig”, location: { state: “MA” } } )
;
// 结果: // =>
{ name: “John”, last: “Resig”, location: { state: “MA” } }
// 深度嵌套
$.extend( true, { name: “John”, location: { city: “Boston” } }, { last: “Resig”, location: { state: “MA” } } );
// 结果 // =>
{ name: “John”, last: “Resig”, // location: { city: “Boston”, state: “MA” } }
3,给jQuery添加静态方法
$.extend({ add:function(a,b){return a+b;}, minus:function(a,b){return a-b}, multiply:function(a,b){return a*b;}, divide:function(a,b){return Math.floor(a/b);} }); //调用 var sum = $.add(3,5)+$.minus(3,5)+$.multiply(3,5)+$.divide(5,7);