首页 > 网站 > WEB开发 > 正文

Ext JS学习第九天 Ext基础之 扩展原生的javascript对象

2024-04-27 14:19:53
字体:
来源:转载
供稿:网友

Ext JS学习第九天 Ext基础之 扩展原生的javascript对象

此文来记录学习笔记;

•Ext对于原生的Javascript对象进行了一系列的扩展,我们把他们掌握好,更能深刻的体会Ext的架构,从而对我们的web开发更好的服务,源码位置,我们可以从开发包的这个位置找到这几个扩展的js源码:•extjs-4.1.1/src/core/src/lang/•ExtJS扩展原生Javascript–Ext.Object–Ext.Number–Ext.String–Ext.Array–Ext.Function–Ext.Date–Ext.Error-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------Ext.Objectno.1 chain(Object object)
//Ext对于原生javascript对象的扩展    //Ext.Object    //1:chain 把当前传入的对象 当成新创建对象的原型        var obj = {            name:'z3',            age:10        };        var result = Ext.Object.chain(obj);        alert(result.name);//z3        alert(result.age);//10        alert(result.hasOwnPRoperty('name')); //false    hasOwnProperty()用于判断属性是否是自己的,返回false说明name属性不是自己的,是obj的;obj是result的原型对象

no.2 each(Object object, Function fn, [Object scope])

//2:each 变量当前对象 然后毁掉函数中暴露出三个属性 key、value、self 如果回调函数返回false则停止迭代        var obj = {            name:'张三' ,             age:20 ,            sex:'男'        };        Ext.Object.each(obj,function(key , value , self){            alert(key + ' : ' + value);            if(age == 20){                return false ;            }        });

no.3fromQueryString(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']]}

no.4 getKey(Object object, Object value)

var person = {    name: 'Jacky',    loves: 'food'};alert(Ext.Object.getKey(person, 'food')); // 弹出 'loves'

no.5 toQueryObjects(String name, Object/Array value, [Boolean recursive]) : Array

将一个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 },];

no.6 getKeys(Object object) : String[]

获取所有对象的key组成的数组

var values = Ext.Object.getKeys({    name: 'Jacky',    loves: 'food'}); // ['name', 'loves']

no.7 getSize(Object object) : Number

获取此对象的所有自有属性的数目

var size = Ext.Object.getSize({    name: 'Jacky',    loves: 'food'}); // size 等于 2

no.8 getValues(Object object) : Array获取给定对象所有的值组成的数组。

var values = Ext.Object.getValues({    name: 'Jacky',    loves: 'food'}); // ['Jacky', 'food']

no.9 merge(Object destination, Object... object) : Object

递归的合并任意数目的对象,但是不引用他们或他们的子对象。看栗子

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    }}

no.10 toQueryObjects(String name, Object/Array value, [Boolean recursive]) : Array举个栗子:

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 },];

no.11 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

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

–Ext.Number

constrain(Number number, Number min, Number max) : Number

检查给定的数值是否在约束的范围内,如果再范围内就返回此数值。否则,如果大于最大值则返回最大值,如果小于最小值则返回最小值

注意本方法不改变给定的数值本身

randomInt(Number from, Number to) : Number

返回一个随机数

toFixed(Number value, Number precision)

小数取舍

alert(Ext.Number.toFixed(3.1415926,5));//结果为 3.142

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------–Ext.String

capitalize(String string) : String

返回首字母大写的字符串

trim(String string) : String

看栗子

var s = '  foo bar  ';alert('-' + s + '-');         //alerts "- foo bar -"alert('-' + Ext.String.trim(s) + '-');  //alerts "-foo bar-
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表