JavaScript能够实现的面向对象的特征有:
·公有属性(public
field)
·公有方法(public Method)
·私有属性(private
field)
·私有方法(private field)
·方法重载(method
overload)
·构造函数(constructor)
·事件(event)
·单一继承(single
inherit)
·子类重写父类的属性或方法(override)
·静态属性或方法(static
member)
<script type="text/javascript"> Object.prototype.Property = 1; Object.prototype.Method = function () { alert(1); } var obj = new Object(); alert(obj.Property); //1 obj.Method(); //1 </script>
<script type="text/javascript"> var obj = new Object(); obj.prototype.Property = 1; //Error //Error obj.prototype.Method = function() { alert(1); } </script>
<script type="text/javascript"> Object.Property = 1; Object.Method = function() { alert(1); } alert(Object.Property); //1 Object.Method(); //1 </script>
<script type="text/javascript"> function Aclass() { this.Property = 1; this.Method = function() { alert(1); } } var obj = new Aclass(); alert(obj.Property); //1 obj.Method(); //1 </script>
<script type="text/javascript"> function Aclass() { this.Property = 1; this.Method = function() { alert(1); } } Aclass.prototype.Property2 = 2; Aclass.prototype.Method2 = function() { alert(2); } var obj = new Aclass(); alert(obj.Property2); //2 obj.Method2(); //2 </script>
<script type="text/javascript">
</script>
<script type="text/javascript"> function Aclass() { this.Property = 1; this.Method = function() { alert(1); } } var obj = new Aclass(); obj.Property = 2; obj.Method = function() { alert(2); } alert(obj.Property); //2 obj.Method(); //2 </script>
<script type="text/javascript"> function AClass() { this.Property = 1; this.Method = function() { alert(1); } } function AClass2() { this.Property2 = 2; this.Method2 = function() { alert(2); } } AClass2.prototype = new AClass(); var obj = new AClass2(); alert(obj.Property); //1 obj.Method(); //1 alert(obj.Property2); //2 obj.Method2(); //2 </script>
<script type="text/javascript"> function AClass() { this.Property = 1; this.Method = function() { alert(1); } } function AClass2() { this.Property2 = 2; this.Method2 = function() { alert(2); } } AClass2.prototype = new AClass(); AClass2.prototype.Property = 3; AClass2.prototype.Method = function() { alert(4); } var obj = new AClass2(); alert(obj.Property); //3 obj.Method(); //4 </script>
<script type="text/javascript">
function Aclass()
{ this.Property = 1; this.Method = function() { alert(1); } } Aclass.prototype.Property = 2; Aclass.prototype.Method = function() { alert(2); } var obj = new Aclass(); alert(obj.Property); //1 obj.Method(); //1
</script>
转载:http://blog.csdn.net/it_man/article/details/6731644
参考:http://blog.csdn.net/chaojie2009/article/details/6719353
[转] js prototype详解,布布扣,bubuko.com
原文:http://www.cnblogs.com/linxuehan/p/3605524.html