Javascript初级 JavaScript进阶

拷贝父类对象的属性


示例代码


<html>

<head>

    <meta charset=”utf-8>

    <title></title>

</head>

<body>

<script type=”text/javascript”>

      Function extend(child,parent){

        Var F=function(){};

        F.prototype=parent.prototype;

        Child.prototype=new F();

        Child.prototype=constructor=child;

        Child.uber=parent.prototype;

  }

Function extend1(child,parent){

  Var p=parent.prototype;

  Var c=child.prototype;

  For(var i in p){

      C[i] =p[i];

}

C.uber=p;

}

Var shape=function(){};

Var TwDSshape=function(){};

Shape.prototype.name=’shape’;

Shape.prototype.tostring=function(){

     Return this.name;

}

Extend(TwDSshape,shape);

Var td=new TwDSshape();

Console.log(td.name);

Console.log(TwDSshape.prototype.name);

Console.log(td._proto_.name);

Console.log(td.hasownproperty(‘name’));

Console.log(td_proto_.hasownproperty(‘name’));

Extend1(TwDSshape,shape);

Var td=new TwDSshape();

Console.log(td_proto_.hasownproperty(‘name’));

Console.log(td_proto_.hasownproperty(‘tostring’));

Console.log(td_proto_.tostring===shape.property.tostring);

 

Var a=function(){};

Var b=function(){};

A.prototype.text=[1,2,3];

A.prototype.text=’this is a text’;

Extend1(b,a);

Console.log(b.property.hasownproperty(‘text’));

Console.log(b.property.hasownproperty(‘text’));

Console.log(b.property. text);

Console.log(b.property.text===a.prototype.text);

B.prototype.text1=’hello maiz’;

Console.log(a.property. text);

B.prototype.text.push(4,5,6);

Console.log(b.property. text);

Console.log(a.property. text);

 

 

B.prototype.text1=[‘aa’,’bb’,’cc’,’dd’];

Console.log(b.property. text);

Console.log(a.property. text);

 

        </script>

</body>

</html>

 

执行结果


javascript1.png 



【本文由麦子学院独家原创,转载请注明出处并保留原文链接】

logo
© 2012-2016 www.maiziedu.com
蜀ICP备13014270号-4 Version 5.0.0 release20160127

您有一个400抵2000现金红包未使用!

客服热线 400-862-8862

回到顶部