一段根据jquery源代码改变的程序相当难理解高手指点下搞了一整天了!!!

renzaijiang 2009-04-20 09:17:24
<script> 
classa=function(array)
{
return new classa.prototype.init(array);
}

classa.prototype={
init:function(array)
{
return this.setArray(array);
},
setArray : function(elems) {
this.length = 0;
Array.prototype.push.apply(this, elems);
return this;
}

}
classa.prototype.init.prototype=classa.prototype;
alert(classa([1,2,3])[0]);//classa([1,2,3])具有数组特性
alert(typeof(classa([1,2,3]));//类型确是object
</script>

这段程序到底是怎么工作的 其中new和this这两个词的工作方式真的很难理解


...全文
98 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
牙签是竹子的 2009-04-21
  • 打赏
  • 举报
回复
up,看看json格式
monexus 2009-04-21
  • 打赏
  • 举报
回复
建议楼主学习一下apply方法,就会明白了
renzaijiang 2009-04-20
  • 打赏
  • 举报
回复
可是他替换的是 push方法而已
为什么自己变数组了
setArray : function(elems) {
this.length = 0;
Array.prototype.push.apply(this, elems);
return this; //这里指向的是classa.prototype吗
}

classa([1,2,3])难道就这样变数组了吗 他只复制了Array的push方法而已啊
no_fengzi 2009-04-20
  • 打赏
  • 举报
回复
那其实是 js里静态类的 一种写法 。建议研究一下json格式
王集鹄 2009-04-20
  • 打赏
  • 举报
回复
本身数组就是一个object类型

typeof 只会返回boolean、string、function、undefined、number、object、unknown

不会返回array。

87,992

社区成员

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

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