日期:2014-05-16  浏览次数:20393 次

javascript继承父类属性数组问题

extend-array.png 扩展名改为htm即可

?

注:父类属性是对象时可采用深度克隆的方式

?

?<html>
? <body>
? <script>
? ??? function A(){
? ??? ??? this.list = [1,2,3];
? ??? ??? this.elem = {a:1,b:2};
? ??? }
? ??? function B(){
? ??? ??? this.list = new Array(B.prototype.list);
? ??? ??? this.elem = new Object(B.prototype.elem);
? ??? }
? ??? B.prototype=new A();
? ??? B.constructor=B;
? ???
? ??? var a1 = new B();
? ??? var a2 = new B();
? ???
? ??? a1.list.push(123);
? ??? a1.elem.a=3;
? ??? alert(a2.list);
? ??? alert(a1.list);
? ??? alert(a2.elem.a);
? ??? alert(a1.elem.a);
? ???
? ???
? </script>
? </body>
</html>