<script type="text/javascript">
        (function () {
            var customService = function () {
                
            };
            customService.prototype = {
                open: function () {
                   contents: this._getHtml(),
                },
                close: function () {
                   
                },
                _getHtml: function () {
                    var html = [];
                    Array.prototype.push.call(html,
                        '<div class=\"content\">',
                            '<div>1、</div>',
                            '<div>2、<\/div>',
                            '<div>3、<\/div>',
                            '<div>4、<\/div>',
                        '<\/div>'
                    );
                    return html.join('');
                }
            };
            window.CustomService = new customService();
        })();
    </script>
1、请问Array.prototype.push.call这个是什么意思??
2、window.CustomService = new customService();这样做有什么好处???
3、还有为什么
(function(){
})();
这样的匿名函数会立即执行?

解决方案 »

  1.   

    问题1: 那句话相当于var html = []; html.push(那一大堆)
    问题2: 全局变量的声明 就像alert一样 可以直接使用 其本质就是window.alert
    问题3:相当于 var aaa = function(){}  aaa();
      

  2.   

    第三个不对吧,相当于var noname=function(){};var aaa=noname();这样
      

  3.   

    两次出现了prototype 是不是一样的意思
    大哥,第一个问题能不能再解释解释??
      

  4.   

    prototype就是原型的意思,类似与对象的self,就是即便你不实例化,它也是有的,也是可以调用的
    Array.prototype.push的意思是调用数组对象原型里的push方法,push你知道吧?在数组尾压入一个元素,用call来调用就是调用push方法的对象是call的第一个参数,即html,其实就相当于html.push();
      

  5.   


    <script type="text/javascript">
           /*这个是匿名函数,即立即执行函数,相当于声明一个函数让他立即执行,具体如1楼大神问题三描述那样。这里用匿名函数包裹下面执行的代码的用意是创建一个执行环境,防止像customService 这样的变量成为全局变量。*/
            (function () {                      
                var customService = function () {
                    
                };
                customService.prototype = {
                    open: function () {
                       contents: this._getHtml(),
                    },
                    close: function () {
                       
                    },
                    _getHtml: function () {
                        var html = [];
                         /*call方法的用意是指定函数执行的上下文,方法的第一个参数即新的函数上下文,也就是说我可以用html去调用push方法,第二个参数是传入push方法的参数*/
                        Array.prototype.push.call(html, 
                            '<div class=\"content\">',
                                '<div>1、</div>',
                                '<div>2、<\/div>',
                                '<div>3、<\/div>',
                                '<div>4、<\/div>',
                            '<\/div>'
                        );
                        return html.join('');
                    }
                };
                /*这里相当于给customService类创建一个namespace,因为window对象可以省略,以后你在使用customService类时可以这样CustomService.customService*/
                window.CustomService = new customService();
            })();
        </script>
      

  6.   

    谢谢了,还有一点,我看很多地方都用array.prototype
        例如:for(var name in Array.prorotype){
                 console.log(name);
              }
    那单独用Array.prototype是什么意思,和Array有什么区别。
      

  7.   

    for(var name in Array.prorotype){
      console.log(name);
      }这是遍历访问Array的原型属性名称