标题:学习中遇到难懂的代码。。希望有人能解释一下。。
只看楼主
yaserwang
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2008-8-20
 问题点数:0 回复次数:0 
学习中遇到难懂的代码。。希望有人能解释一下。。
我是新手,看书的时候碰到一段代码,不大明白。。希望有好心人能解释一下。。谢谢。。
书名:The Art & Science of JavaScript
其中的第五章,Metaprogramming

程序代码:
['forEach', 'map', 'filter', 'slice', 'concat'].forEach(function(func) {
    // test if it exists already and only create if it doesn't
    if (!Array[func]) Array[func] = function(object) {
      // use the call trick to slice() the first argument off the argument list
      // as that is going to be the object we operate on
      var newArgs = Array.prototype.slice.call(arguments, 1);
      // call the array function with object as this with the arguments we just created
      return this.prototype[func].apply(object, newArgs);
    }
});


前面两句我明白是什么意思,从['forEach', 'map', 'filter', 'slice', 'concat']这个数列循环,每一个都赋一个方法,但是function(object)中那个object parameter 我就不知道是用来干吗的,然后后面newArgs就变成了['map', 'filter', 'slice', 'concat'],return后面又不明白了。。
搜索更多相关主题的帖子: 代码 解释 学习 
2008-08-20 22:06



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-229226-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 1.384067 second(s), 8 queries.
Copyright©2004-2025, BCCN.NET, All Rights Reserved