原型里定义属性方法

cutemurphy 2011-11-24 01:33:28
function A( ){
this.c=function( ){
alert(10);
};
this.d=10;
}
A.prototype={
qq:this.c,
mm:function( ){
alert(this.d);
}
}
var aa=new A( );
aa.mm( ); // 可以
aa.qq( ); // 错误

同样在原型里,一个可以,一个不可以,为何?
...全文
132 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
ziyouren521125 2011-11-24
  • 打赏
  • 举报
回复

A.prototype={
qq:this.c,
mm:function( ){
alert(this.d);
}
}


这块不正确吧,this在这里面不能指向A()的
默默不得鱼 2011-11-24
  • 打赏
  • 举报
回复
你的
A.prototype={
qq:this.c,
mm:function( ){
alert(this.d);
}
}
把A的prototype指向了后面这个对象,
之前那个function A已经跟A没有任何关系了
liangws 2011-11-24
  • 打赏
  • 举报
回复
A.prototype={
qq:this.c,//这里的this绑定的是window
mm:function( ){
alert(this.d);
}
}
abcd_ufo 2011-11-24
  • 打赏
  • 举报
回复

首先程序得预编译一次.. 也就是说程序在未执行 A 函数的时候, A.property 里面的qq 属性的值已经附上了 在赋值的时候 因为不存在this.c 所以 this.c 就是undifined ..

我说的不是很标准 ..
MuBeiBei 2011-11-24
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 cutemurphy 的回复:]

this.d为何可以找到。。。。。。。。。。。。。。。。。。。``
[/Quote]

mm:function( ){
alert(this.d);
}

你外面写的 function
cutemurphy 2011-11-24
  • 打赏
  • 举报
回复
this.d为何可以找到。。。。。。。。。。。。。。。。。。。``
MuBeiBei 2011-11-24
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 cutemurphy 的回复:]

为毛还要包一层?
[/Quote]

qq:this.c

根本找不到~·
cutemurphy 2011-11-24
  • 打赏
  • 举报
回复
为毛还要包一层?
MuBeiBei 2011-11-24
  • 打赏
  • 举报
回复
<script language="javascript">
function A( ){
this.c=function( ){
alert(10);
};
this.d=10;
}
A.prototype={
qq:function(){this.c()},
mm:function( ){
alert(this.d);
}
}
var aa=new A( );
aa.mm( ); // 可以
aa.qq(); // 错误


</script>

87,989

社区成员

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

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