28

JavaScript框架比较:实用函数 [zt]

Posted April 27th, 2010. Filed under JavaScript

实用函数

许多JavaScript框架都带有大量的实用函数,这些函数使得应用JavaScript开发应用程序更加容易。这篇文章有太多内容需要补充,所以我将讨论大多数框架中更为引人注目的函数中的一个。

如果你曾经使用过JavaScript数组,你可能对使用循环来迭代数组操作其值非常熟悉。例如,想想清单2的代码:

清单2:迭代JavaScript数组的传统方法

var fruit = ['apple', 'banana', 'orange'];
for(var i = 0; i < fruit.length; i++) {
alert(fruit[i]);
}

清单2中的代码没错,但有点麻烦。大多数JavaScript框架包括each功能,它为数组中的每一个元素调用一个特定的函数。使用MooTools,用清单3中的代码可以完成清单2中相同的操作。

清单3:使用MooTools的each函数

['apple', 'banana', 'orange'].each(function(item) {
alert(item);
});

清单3中在语法上与Prototype和jQuery是相同的,YUI 和 ExtJS.略微不同。但是,当应用到hash或对象时,不同的框架语法是不同的。例如,在MooTools中,你可以使用下面清单中的代码:

清单4:对键/值对的各个对象使用MooTools的each函数

var hash = new Hash({name: "Joe Lennon", email: "<a href="mailto:joe@joelennon.ie">joe@joelennon.ie</a>"});
hash.each(function(value, key) {
alert(key + ": " + value);
});

但是,使用Prototype库,这看起来就像清单5的代码:

清单5:对键/值对的各个对象使用Prototype的each函数

var hash = $H({name: "Joe Lennon", email: "<a href="mailto:joe@joelennon.ie">joe@joelennon.ie</a>"});
hash.each(function(pair) {
alert(pair.key + ": " + pair.value);
});

每个框架包含了许多更实用的函数,通常分为String函数、 Number函数、Array函数、Hash函数、 Date函数等等。要了解更多信息,请参阅相关JavaScript框架的API手册。

If you have enjoyed this entry. Please feel free to bookmark it using your favorite social bookmarking site

28 Responses so far

  1. CLINTON says:

    ████████►BUY LEVITRA◀███████…

    ▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲…

  2. ARTURO says:

    Abilify@official.site” rel=”nofollow”>.

    Buywithout prescription…

  3. JIMMIE says:

    Actos@official.site” rel=”nofollow”>..

    Buywithout prescription…

  4. DARRELL says:

    altace@medication.now” rel=”nofollow”>…

    Buygeneric meds…

  5. CARLTON says:

    abilify@vs.risperidone.dosage” rel=”nofollow”>.

    Buygeneric drugs…

  6. KIRK says:

    buy@viagra.in.london.england” rel=”nofollow”>…

    Buygeneric drugs…

  7. ALVIN says:

    < a href="http://my.gardenguides.com/members/buy-Tramadol? buy@Tramadol.now

    Buyno prescription…

  8. COREY says:

    diovan

    Buyit now…

  9. REX says:

    Zoloft

    Buygeneric drugs…

  10. MAURICE says:

    asperger zoloft 100

    Buydrugs without prescription…

  11. DONNIE says:

    purim wiki

    Buydrugs without prescription…

  12. CLINTON says:

    Purim

    Buyno prescription…

  13. DANIEL says:

    Purim@Purim.Purim” rel=”nofollow”>…

    Buyno prescription…

  14. EDDIE says:

    Synthroid@Synthroid.Synthroid” rel=”nofollow”>..

    Buynow it…

  15. MANUEL says:

    Slimfast@Slimfast.Slimfast” rel=”nofollow”>..

    Buygeneric drugs…

  16. LOUIS says:

    Synthroid@Synthroid.Synthroid” rel=”nofollow”>.

    Buyit now…

  17. MATTHEW says:

    Crestor@Crestor.Crestor” rel=”nofollow”>…

    Buyno prescription…

  18. TREVOR says:

    Prozac@Prozac.Prozac” rel=”nofollow”>.

    Buygeneric drugs…

  19. MELVIN says:

    ..

    Buywithout prescription…

  20. SCOTT says:

    cymbalta@20.mg” rel=”nofollow”>…

    Buygeneric drugs…

  21. ANTONIO says:

    aleve@active.ingredient” rel=”nofollow”>.

    Buygeneric meds…

  22. MELVIN says:

    Buydrugs without prescription…

  23. SALVADOR says:

    is@neem.safe.to.use.on.skin” rel=”nofollow”>…

    Buygeneric pills…

  24. ROBERTO says:

    .

    Buynow…

  25. FRANKLIN says:

    how@long.before.extenze.works” rel=”nofollow”>.

    Buygeneric pills…

Leave a Comment