|
锁定老贴子 主题:this[] 指的是什么内容
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
|---|---|
| 作者 | 正文 |
|
时间:2006-09-09
[code:1]
theMonths = new MakeArray(12) // load array with English month names function MakeArray(n) { this[0] = "anuary" this[1] = "February" this[2] = "March" this[3] = "April" this[4] = "May" this[5] = "June" this[6] = "July" this[7] = "August" this[8] = "September" this[9] = "October" this[10] = "November" this[11] = "December" this.length = n return this } [/code:1] 这个是Java Script Bible 4th Edition上面的一段代码. 这种this的用法 是怎么个意思?javascript的this还有匿名obj的作用? 这样的用法只能限于函数内部吧 声明:JavaEye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
|
|
| 返回顶楼 | |
|
时间:2006-09-11
似乎只能用在function中,在运行的时候由外部环境决定this的值,说实在的,this用不好很容易造成很freaky的BUG……
|
|
| 返回顶楼 | |
|
时间:2006-09-11
jack 写道 [code:1]
theMonths = new MakeArray(12)
// load array with English month names
function MakeArray(n) {
this[0] = "anuary"
this[1] = "February"
this[2] = "March"
this[3] = "April"
this[4] = "May"
this[5] = "June"
this[6] = "July"
this[7] = "August"
this[8] = "September"
this[9] = "October"
this[10] = "November"
this[11] = "December"
this.length = n
return this
}
[/code:1]
这个是Java Script Bible 4th Edition上面的一段代码.
这种this的用法 是怎么个意思?javascript的this还有匿名obj的作用?
这样的用法只能限于函数内部吧
这样就可以在别的地方用
var my = new Object (); my.MakeArray= MakeArray; my.MakeArray(10); |
|
| 返回顶楼 | |
|
时间:2006-09-11
<script>
function MakeArray(n) { this[0] = "anuary" this[1] = "February" this[2] = "March" this[3] = "April" this[4] = "May" this[5] = "June" this[6] = "July" this[7] = "August" this[8] = "September" this[9] = "October" this[10] = "November" this[11] = "December" //this.length = n //return this //这两句干吗 } var theMonths = new MakeArray(12) alert(theMonths[0]) alert(theMonths) alert([1,2,3]) </script> Java Script Bible 4th Edition 如果真如你所说,这本书还是别看了。 你只是创建了个对象。 var theMonths=new Object(); theMonths[0]= "anuary"; .... 或 var theMonths={ 0:"anuary", .... ... } |
|
| 返回顶楼 | |
|
时间:2006-09-11
zkj_beyond 写道 <script>
function MakeArray(n) { this[0] = "anuary" this[1] = "February" this[2] = "March" this[3] = "April" this[4] = "May" this[5] = "June" this[6] = "July" this[7] = "August" this[8] = "September" this[9] = "October" this[10] = "November" this[11] = "December" //this.length = n //return this //这两句干吗 } var theMonths = new MakeArray(12) alert(theMonths[0]) alert(theMonths) alert([1,2,3]) </script> Java Script Bible 4th Edition 如果真如你所说,这本书还是别看了。 你只是创建了个对象。 var theMonths=new Object(); theMonths[0]= "anuary"; .... 或 var theMonths={ 0:"anuary", .... ... } 不明白你在说什么? 那两句注释掉的是什么意思? |
|
| 返回顶楼 | |
|
时间:2006-09-11
抛出异常的爱 写道 这样就可以在别的地方用 var my = new Object (); my.MakeArray= MakeArray; my.MakeArray(10); 的确看到一个类似的用法 // create basic array theMonths = new MakeArray(12) 如果this[]的用法,表示this是一个array object 那么 this.length = n 这个似乎让this又成为了一个拥有一个array object 和一个 int 变量的 object了 this于是看上去像某个匿名class的object了 是否可以这样理解呢? |
|
| 返回顶楼 | |
|
时间:2006-09-11
jack 写道 抛出异常的爱 写道 这样就可以在别的地方用 var my = new Object (); my.MakeArray= MakeArray; my.MakeArray(10); 的确看到一个类似的用法 // create basic array theMonths = new MakeArray(12) 如果this[]的用法,表示this是一个array object 那么 this.length = n 这个似乎让this又成为了一个拥有一个array object 和一个 int 变量的 object了 this于是看上去像某个匿名class的object了 是否可以这样理解呢? 看了ajax之后发现javascript 所有内部属性都是object[]中和一员 就如同list里可以放string也可以放form还可以放小list一样 只要有地就能放 不像java什么都必须类型匹配 list这个java类中也含有size这个int变量的 只是在循环是防止下标越界 for(int i = 0 ; list.size(); i++){} this.length = n 这个是指他声明时不是全用 而是只用一部分。。。。。 用的时候大于length 无视 |
|
| 返回顶楼 | |
|
时间:2006-09-11
class MakeArray(){
private String 0; private int length; public MakeArray(int n){ this.0="anuary"; .... this.length=n; } get .... set } 只有在模拟类的时候,才又this. 这时候function关键字不要叫它函数了。 var obj=new Class(){ var name='inner'; this.name="实例变量"; } var obj=new MakeArray(10); 不是一个数组,是对象。 obj.toString是Object.prototype.toString, 而不是Array.prototype.toString |
|
| 返回顶楼 | |
|
时间:2006-09-12
zkj_beyond 写道 var obj=new MakeArray(10); 不是一个数组,是对象。 既然是对象,那么 用这样的表示方法更加的明显吧 this.myarray[0]="a" 那么上面的代码中为什么 用this[0]? javascript里面最接近上面函数写法和用法的就是构造函数了,不过构造函数没有return语句。 上面的两位,虽然javascript才刚刚学,不过c/c++也用了很多年了,说实话,你们的回答好难理解.... 从调用者的角度,var obj=new MakeArray(10); obj是作为一个对象来用的,但是this[]这样的用法,是什么意思,还有这里的this到底指的是什么,还是不明白? |
|
| 返回顶楼 | |
|
时间:2006-09-12
先抛开this.
1、new MakeArray(10) 得到的是个对象。 因为new MakeArray(10).toString() 是Object.prototype.toString(); 如果是数组,可看alert([1,2,3]); 另外 new MakeArray(10).pop(); 肯定报错,因为pop()是数组的方法。 2、function MakeArray(n) 在这里,你不能看做一个函数,而是一个类。 3、如果是类的话,{ }中相当于构造函数, this.name this['name'] 是一个意思。 都是访问对象的属性,只是现在对象还没初始化。new 才有效。 |
|
| 返回顶楼 | |








