Array.prototype.push.call是什么意思啊

KeenWon 2011-12-08 11:32:24

<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(){
})();
这样的匿名函数会立即执行?
...全文
379 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
KeenWon 2011-12-08
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 zhang6464 的回复:]

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

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

咸鱼boris 2011-12-08
  • 打赏
  • 举报
回复

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

zhang6464 2011-12-08
  • 打赏
  • 举报
回复
prototype就是原型的意思,类似与对象的self,就是即便你不实例化,它也是有的,也是可以调用的
Array.prototype.push的意思是调用数组对象原型里的push方法,push你知道吧?在数组尾压入一个元素,用call来调用就是调用push方法的对象是call的第一个参数,即html,其实就相当于html.push();
[Quote=引用 3 楼 semanwmj 的回复:]

引用 1 楼 fanchuanzhidu 的回复:

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

两次出现了prototype 是不是一样的意思
大哥,第一个问题能不能……
[/Quote]
KeenWon 2011-12-08
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 fanchuanzhidu 的回复:]

问题1: 那句话相当于var html = []; html.push(那一大堆)
问题2: 全局变量的声明 就像alert一样 可以直接使用 其本质就是window.alert
问题3:相当于 var aaa = function(){} aaa();
[/Quote]
两次出现了prototype 是不是一样的意思
大哥,第一个问题能不能再解释解释??
zhang6464 2011-12-08
  • 打赏
  • 举报
回复
第三个不对吧,相当于var noname=function(){};var aaa=noname();这样[Quote=引用 1 楼 fanchuanzhidu 的回复:]

问题1: 那句话相当于var html = []; html.push(那一大堆)
问题2: 全局变量的声明 就像alert一样 可以直接使用 其本质就是window.alert
问题3:相当于 var aaa = function(){} aaa();
[/Quote]
豆虫 2011-12-08
  • 打赏
  • 举报
回复
问题1: 那句话相当于var html = []; html.push(那一大堆)
问题2: 全局变量的声明 就像alert一样 可以直接使用 其本质就是window.alert
问题3:相当于 var aaa = function(){} aaa();
oggmm 2011-12-08
  • 打赏
  • 举报
回复
for(var name in Array.prorotype){
console.log(name);
}

这是遍历访问Array的原型属性名称

87,993

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧