加入收藏 | 设为首页 | 会员中心 | 我要投稿 湖南网 (https://www.hunanwang.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 创业 > 正文

Extjs中的迭代要领

发布时间:2018-10-13 11:57:50 所属栏目:创业 来源:站长网
导读:EXTJS 有许多的迭代要领,譬喻,你大概已知道的Ext.each,但尚有其它一些不为人知且很有效的要领。起首,扼要回首下Ext.each: Ext.each 为每一个数组的成员应用统一个要领,它根基上是一个更利便的轮回情势 var people = ['Bill', 'Saul', 'Gaius'];//using

EXTJS 有许多的迭代要领,譬喻,你大概已知道的Ext.each,但尚有其它一些不为人知且很有效的要领。起首,扼要回首下Ext.each:

Ext.each

为每一个数组的成员应用统一个要领,它根基上是一个更利便的轮回情势

var people = ['Bill', 'Saul', 'Gaius'];

//using each to detect Cylons:
Ext.each(people, function (person, index)
{
    var cylon = (index + 1) % 2 == 0; //every second man is a toaster
    alert(person + (cylon ? ' is ' : ' is not ') + 'a fraking cylon');
});

//is the same as
for (var i = 0; i < people.length; i++)
{
    var person = people[i];
    var cylon = (index + 1) % 2 == 0; //every second man is a toaster

    alert(person + (cylon ? ' is ' : ' is not ') + 'a frakin cylon');
};

Ext.iterate Ext.iterate 与 Ext.each 相同针对非数组工具. 凡是用在for-in 轮回中:
var ships = { 'Bill': 'Galactica', 'Laura': 'Colonial One' };

Ext.iterate(ships, function (key, value)
{
    alert(key + "'s ship is the " + value);
});

//is the same as
for (key in ships)
{
    var value = ships[key];
    alert(key + "'s ship is the " + value);
}

用Ext.iterate在数组上,与Ext.each完全沟通。
each和iterate要领都有第三个可选参数scope。
另一个有效的能力是你可以更利便的重用沟通的要领:

var myFunction = function (item, index)
{
    //does some clever thing
}

Ext.each(people, myFunction);
Ext.each(['another', 'array'], myFunction);

Ext.pluck (4.0.0之后过期) Ext.pluck从工具数组捕捉特定的属性
var animals = [
  { name: 'Ed', species: 'Unknown' },
  { name: 'Bumble', species: 'Cat' },
  { name: 'Triumph', species: 'Insult Dog' }
];

Ext.pluck(animals, 'species'); //returns ['Unknown', 'Cat', 'Insult Dog']
Ext.pluck(animals, 'name'); //returns ['Ed', 'Bumble', 'Triumph']

此要领自4.0.0不提议行使,请用Ext.Array.pluck取代.

Ext.invoke

(4.0.0之后过期)数组中全部成员挪用统一个要领,并返回功效,行使用上例animals:

var describeAnimal = function (animal)
{
    return String.format("{0} is a {1}", animal.name, animal.species);
}

var describedAnimals = Ext.invoke(animals, describeAnimal);
console.log(describedAnimals); // ['Ed is a Unknown', 'Bumble is a Cat', 'Triumph is a Insult Dog'];

Ext.invoke与Ruby的荟萃要领相同,使得更轻易转换数组,任何增进的参数都可通过Ext.invoke转达。
此要领自4.0.0不提议行使,4.X系列版本后将被移除。

Ext.Partition

Ext.Partition将数组拆分成两部门。

var trees = [
  { name: 'Oak', height: 20 },
  { name: 'Willow', height: 10 },
  { name: 'Cactus', height: 5 }
];

var isTall = function (tree) { return tree.height > 15 };

Ext.partition(trees, isTall);

//returns:
[
  [{ name: 'Oak', height: 20}],
  [{ name: 'Willow', height: 10 }, { name: 'Cactus', height: 5}]
]

此要领自4.0.0不提议行使,4.X系列版本后将被移除。

数学要领
var numbers = [1, 2, 3, 4, 5];
Ext.min(numbers); //1
Ext.max(numbers); //5
Ext.sum(numbers); //15
Ext.mean(numbers); //3

原文地点:Ext JS iterator functions

(编辑:湖南网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    热点阅读