JavaScript能夠實現的面向對象的特征有:
·公有屬性(public field)
·公有方法(public Method)
·私有屬性(private field)
·私有方法(private field)
·方法重載(method overload)
·構造函數(constructor)
·事件(event)
·單一繼承(single inherit)
·子類重寫父類的屬性或方法(override)
·靜態屬性或方法(static member)
例子一(JavaScript中允許添加行為的類型):可以在類型上使用proptotype來為類型添加行為。這些行為只能在類型的實例上體現。 JS中允許的類型有Array, Boolean, Date, Enumerator, Error, Function, Number, Object, RegExp, String
Js代碼
- <script type="text/javascript">
- Object.prototype.Property = 1;
- Object.prototype.Method = function ()
- {
- alert(1);
- }
- var obj = new Object();
- alert(obj.Property);
- obj.Method();
- </script>
<script type="text/javascript">Object.prototype.Property = 1;Object.prototype.Method = function (){ alert(1);} var obj = new Object();alert(obj.Property);obj.Method();</script>
例子二(prototype使用的限制):在實例上不能使用prototype,否則發生編譯錯誤
Js代碼
- <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">var obj = new Object();obj.prototype.Property = 1; //Error//Errorobj.prototype.Method = function(){ alert(1);}</script>
例子三(如何定義類型上的靜態成員):可以為類型定義“靜態”的屬性和方法,直接在類型上調用即可
Js代碼
- <script type="text/javascript">
- Object.Property = 1;
- Object.Method = function()
- {
- alert(1);
- }
- alert(Object.Property);
- Object.Method();
- </script>
<script type="text/javascript">Object.Property = 1;Object.Method = function(){ alert(1);} alert(Object.Property);Object.Method();</script>
例子五():這個例子演示了通常的在JavaScript中定義一個類型的方法
Js代碼
- <script type="text/javascript">
- function Aclass()
- {
- this.Property = 1;
- this.Method = function()
- {
- alert(1);
- }
- }
- var obj = new Aclass();
- alert(obj.Property);
- obj.Method();
- </script>
<script type="text/javascript">function Aclass(){this.Property = 1;this.Method = function(){ alert(1);}}var obj = new Aclass();alert(obj.Property);obj.Method();</script>
例子六(JavaScript中允許添加行為的類型):可以在外部使用prototype為自定義的類型添加屬性和方法。
Js代碼
- <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);
- obj.Method2();
- </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);obj.Method2();</script>
例子八():可以在對象上改變屬性。(這個是肯定的)也可以在對象上改變方法。(和普遍的面向對象的概念不同)
Js代碼
- <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);
- obj.Method();
- </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);obj.Method();</script>
例子九():可以在對象上增加屬性或方法
Js代碼
- <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);
- obj.Method();
- </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);obj.Method();</script>
例子十(如何讓一個類型繼承於另一個類型):這個例子說明了一個類型如何從另一個類型繼承。
Js代碼
- <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);
- obj.Method();
- alert(obj.Property2);
- obj.Method2();
- </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);obj.Method();alert(obj.Property2);obj.Method2();</script>
例子十一(如何在子類中重新定義父類的成員):這個例子說明了子類如何重寫父類的屬性或方法。
Js代碼
- <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);
- obj.Method();
- </script>