关于ExtJS对javascript中的Object的扩展,可以参考其帮助文档,文档下载地址:http://download.csdn.net/detail/z1137730824/7748893
下面对其中的方法进行介绍:
(1)chain,把当前传入的对象当成新创建对象的原型。
实例:
var obj={ name:'张三', age:20 } var result=Ext.Object.chain(obj); alert(result.name);//会返回张三 alert(result.age);//会返回20 alert(result.hasOwnProperty('name'));//会返回false
(2)each
迭代一个对象,在每个迭代上调用给定的回调函数 在回调函数中返回 false
可以停止迭代. 示例:
var person = { name: ‘Jacky‘ hairColor: ‘black‘ loves: [‘food‘, ‘sleeping‘, ‘wife‘] }; Ext.Object.each(person, function(key, value, myself) { console.log(key + ":" + value);//在浏览器控制台中打印person中的属性和值 if (key === ‘hairColor‘) { return false; // 停止迭代 } });
(3)fromQueryString
fromQueryString( String queryString, [Boolean recursive] ) : Object
将查询字符串转换回对象。
不递归:
Ext.Object.fromQueryString("foo=1&bar=2"); // 返回 {foo: 1, bar: 2} Ext.Object.fromQueryString("foo=&bar=2"); // 返回 {foo: null, bar: 2} Ext.Object.fromQueryString("some%20price=%24300"); // 返回 {‘some price‘: ‘$300‘} Ext.Object.fromQueryString("colors=red&colors=green&colors=blue"); // 返回 {colors: [‘red‘, ‘green‘, ‘blue‘]}
递归:
Ext.Object.fromQueryString( "username=Jacky&"+ "dateOfBirth[day]=1&dateOfBirth[month]=2&dateOfBirth[year]=1911&"+ "hobbies[0]=coding&hobbies[1]=eating&hobbies[2]=sleeping&"+ "hobbies[3][0]=nested&hobbies[3][1]=stuff", true); // 返回 { username: ‘Jacky‘, dateOfBirth: { day: ‘1‘, month: ‘2‘, year: ‘1911‘ }, hobbies: [‘coding‘, ‘eating‘, ‘sleeping‘, [‘nested‘, ‘stuff‘]] }
(4)getKey
根据指定的值,返回第一个匹配的key. 如果没有匹配的值,将返回null.
var person = { name: ‘Jacky‘, loves: ‘food‘ }; alert(Ext.Object.getKey(person, ‘food‘)); // 弹出警告 ‘loves‘
(5)getKeys
获取所有对象的key组成的数组
var values = Ext.Object.getKeys({ name: ‘Jacky‘, loves: ‘food‘ }); // [‘name‘, ‘loves‘]
(6)getSize
获取此对象的所有自有属性的数目
var size = Ext.Object.getSize({ name: ‘Jacky‘, loves: ‘food‘ }); // size 等于 2
(7)getValues
获取给定对象所有的值组成的数组。
var values = Ext.Object.getValues({ name: ‘Jacky‘, loves: ‘food‘ }); // [‘Jacky‘, ‘food‘]
(8)merge
递归的合并任意数目的对象,但是不引用他们或他们的子对象。
var extjs = { companyName: ‘Ext JS‘, products: [‘Ext JS‘, ‘Ext GWT‘, ‘Ext Designer‘], isSuperCool: true, office: { size: 2000, location: ‘Palo Alto‘, isFun: true } }; var newStuff = { companyName: ‘Sencha Inc.‘, products: [‘Ext JS‘, ‘Ext GWT‘, ‘Ext Designer‘, ‘Sencha Touch‘, ‘Sencha Animator‘], office: { size: 40000, location: ‘Redwood City‘ } }; var sencha = Ext.Object.merge(extjs, newStuff); // 此时extjs和sencha等于 { companyName: ‘Sencha Inc.‘, products: [‘Ext JS‘, ‘Ext GWT‘, ‘Ext Designer‘, ‘Sencha Touch‘, ‘Sencha Animator‘], isSuperCool: true, office: { size: 40000, location: ‘Redwood City‘, isFun: true } }
(9)toQueryObjects
将一个name
- value
对转换为一个对象数组,支持内部结构的转换,对构造查询字符串非常有用。 示例:
var objects = Ext.Object.toQueryObjects(‘hobbies‘, [‘reading‘, ‘cooking‘, ‘swimming‘]); // objects此时等于: [ { name: ‘hobbies‘, value: ‘reading‘ }, { name: ‘hobbies‘, value: ‘cooking‘ }, { name: ‘hobbies‘, value: ‘swimming‘ }, ]; var objects = Ext.Object.toQueryObjects(‘dateOfBirth‘, { day: 3, month: 8, year: 1987, extra: { hour: 4 minute: 30 } }, true); // 递归 // objects此时等于: [ { name: ‘dateOfBirth[day]‘, value: 3 }, { name: ‘dateOfBirth[month]‘, value: 8 }, { name: ‘dateOfBirth[year]‘, value: 1987 }, { name: ‘dateOfBirth[extra][hour]‘, value: 4 }, { name: ‘dateOfBirth[extra][minute]‘, value: 30 }, ];
(10)toQueryString
toQueryString( Object object, [Boolean recursive] ) : String
将一个对象转换成编码的查询字符串
不递归:
Ext.Object.toQueryString({foo: 1, bar: 2}); // 返回 "foo=1&bar=2" Ext.Object.toQueryString({foo: null, bar: 2}); // 返回 "foo=&bar=2" Ext.Object.toQueryString({‘some price‘: ‘$300‘}); // 返回 "some%20price=%24300" Ext.Object.toQueryString({date: new Date(2011, 0, 1)}); // 返回 "date=%222011-01-01T00%3A00%3A00%22" Ext.Object.toQueryString({colors: [‘red‘, ‘green‘, ‘blue‘]}); // 返回 "colors=red&colors=green&colors=blue"
递归:
Ext.Object.toQueryString({ username: ‘Jacky‘, dateOfBirth: { day: 1, month: 2, year: 1911 }, hobbies: [‘coding‘, ‘eating‘, ‘sleeping‘, [‘nested‘, ‘stuff‘]] }, true); // 返回如下字符串(换行和url-decoded是为了便于阅读的目的): // username=Jacky // &dateOfBirth[day]=1&dateOfBirth[month]=2&dateOfBirth[year]=1911 // &hobbies[0]=coding&hobbies[1]=eating&hobbies[2]=sleeping&hobbies[3][0]=nested&hobbies[3][1]=stuff
Parameters
- object : Object
要编码的对象
- recursive : Boolean (optional)
是否递归的翻译对象,这种格式在 PHP / Ruby on Rails服务器中被类似地支持.
Defaults to:
false
Returns
- String
queryString