js对象属性有两种访问方式,代码如下:
- <span style="font-size:18px;"><html>
- <head>
- <script language="javascript">
- function Person() {};
- var p1 = new Person();
- p1.name = "拉拉";
- window.alert(p1.name);
- var al = "na" + "me";
- window.alert(p1[al]);
- </script>
- </head>
- <body>
- </body>
- </html></span>
js对象初始化的小例子:
- <span style="font-size:18px;"><html>
- <head>
- <script language="javascript">
- function Person(){};
- var a = new Person();
- a.age = 10;
- a.name = "拉拉";
- var b = a;
- b.name = "小赖";
- window.alert(b.age + "b名字:" + b.name + "a名字:" + a.name);
- b = null;
- window.alert(a.age + "a名字:" + a.name);
- </script>
- </head>
- <body>
- </body>
- </html></span>
js中的对象回收机制:初始化一个对象时,对象名放在桟中,它指向堆中存放的内容。在js引擎中会有一个表单,存放堆地址和它被引用的次数,如果次数为0,则被回收。这种方式是被动的,js还提供一种主动释放内存的方式。代码为:
- <span style="font-size:18px;"><html>
- <head>
- <script language="javascript">
- function Person(){};
- var a = new Person();
- a.age = 10;
- a.name = "拉拉";
- var b = a;
- b.name = "小赖";
- window.alert(b.age + "b名字:" + b.name + "a名字:" + a.name);
- b = null;
- window.alert(a.age + "a名字:" + a.name);
- </script>
- </head>
- <body>
- </body>
- </html></span>
delete只是释放对象的属性,不过立即释放,即使还有指向它的对象,也不能访问了,已经被释放掉了。
-
顶