一个精巧的小小问题 js里面想这么定义:function A(){ this.a = 1; this.f = function(){ this.a = 2; }}想用f访问A下的属性a,这么写显示undefined。如何才能实现目的呢? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 你这是要从新给a赋值吗?是的话可以这样this.a = 2; return this.a; function A() { this.a = 1; this.f = function () { this.a = 2; return this.a; } } var a = new A(); alert(a.a) a.f(); alert(a.a)有什么问题吗?你怎么用的 function A(){ this.a = 1; this.f = new B();}function B(){ this.a = 2;}var temp = new A(); alert( temp.f.a); function A() { this.a = 1; var sender = this; this.f = function () { sender.a = 2; } } var c = new A(); c.f(); alert(c.a);// 为2希望可以帮助楼主理解 刚试验了下是这样的,看样子我理解有问题。。我很好奇为啥在function () { this.a = 2; return this.a; }内部的this还是指向了 A ,而不是 A.f呢。请教下 问题解决了!正确用法正如楼上所说。是我的层数太多了没用对。另外在A下的f中使用this,指向的确实还是A而不是f 请教一个小小的正则替换 跪求一个下拉框的js判断 如何将popup出的窗口显示在所有页面之上 javascript中如何获取表格中的当前行号? 请高手指点(急) 一个 Ext JS 2.0 Samples 的问题 查看拼音首字母缩写程序 code by meixx 梅雪香 帮忙看看连接数据库的javascript特效代码 怎么在让用户在浏览器中输入网址以后就谈出一个对话框? 很简单,如何实现? img标签下的onclick不可用 javascript源代码字符过滤问题
是的话可以这样
this.a = 2;
return this.a;
this.a = 1;
this.f = function () {
this.a = 2;
return this.a;
}
}
var a = new A();
alert(a.a)
a.f();
alert(a.a)有什么问题吗?你怎么用的
this.a = 1;
this.f = new B();
}
function B(){
this.a = 2;
}var temp = new A();
alert( temp.f.a);
this.a = 1;
var sender = this;
this.f = function () {
sender.a = 2;
}
} var c = new A();
c.f();
alert(c.a);// 为2
希望可以帮助楼主理解
this.a = 2;
return this.a;
}内部的this还是指向了 A ,而不是 A.f呢。请教下
另外在A下的f中使用this,指向的确实还是A而不是f