日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

JavaScript类的几种写法

發布時間:2023/12/20 javascript 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 JavaScript类的几种写法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

我們常用的有以下幾種方法來用JavaScript寫一個“類”:

?

1. 構造函數(public屬性和方法)

1: function Person(iName, iAge){ 2: this.name=iName; //public 3: this.age=iAge; //public 4: this.ShowStudent=function(){ //public 5: alert(this.name); 6: }; 7: }

以上的屬性和方法都是public的。下面的例子給出private和public的屬性和方法。

?

2. 構造函數(public, private屬性和方法)

1: function Person(iName, iAge){ 2: //private field 3:  var name = iName;     4: var age = iAge; 5:      6: //private method 7:  var privatefn = function(){     8:   alert(name); 9:  } 10: 11: return { 12:    //public field 13: Name: "hello " + name, 14: Age: "hello " + age, 15: 16: ShowStudent: function(){ 17: privatefn();  18: alert(this.Name);  19: } 20: }; 21: }

調用:(new Person("xiao","10")).ShowStudent();

?

?

3. 原型方法(prototype)

1: function c(){} 2: c.prototype={ 3: name: "init value a", 4: setName: function(iName){ 5: this.name=iName; 6: }, 7: getName: function(){ 8: alert('hello from c, name: ' + this.name); 9: } 10: }; 11: (new c).getName(); // 輸出hello from c, name: init value a

?

4. 構造函數+原型方法(prototype)

1: function Person(iName) { 2: this.name = iName; 3: }; 4: ? 5: Person.prototype={ 6: getName: function(){ 7: return this.name; 8: } 9: }; 10: 11: //調用 12: var b = new Person("jack"); 13: alert(b.getName());

?

5. 構造函數+原型方法(prototype)- 節省內存的寫法

1: function Person(iName, iAge){ 2: this.name=iName; 3: this.age=iAge; 4: 5: //對象實例都共享同一份方法不造成內存浪費 6: if(typeof Person._initialized == "undefined"){ 7: Person.prototype.ShowStudent=function(){ 8: alert(this.name); 9: }; 10: Person._initialized=true; 11: } 12: } 13: //調用 14: (new Person("jack","20")).ShowStudent();

以上的實現方法如果不用_initialized的方法,也可以指向一個外部函數,道理一樣。

?

6. JavaScript類的單例(Singleton)模式寫法

1: var MyNamespace = {}; 2: MyNamespace.Singleton = (function() { 3: var uniqueInstance; // Private attribute that holds the single instance. 4: function constructor() { // All of the normal singleton code goes here. 5: // Private members. 6: var privateAttribute1 = false; 7: var privateAttribute2 = [1, 2, 3]; 8: function privateMethod1() { 9: //... 10: } 11: function privateMethod2(args) { 12: //... 13: } 14: return { // Public members. 15: publicAttribute1: true, 16: publicAttribute2: 10, 17: publicMethod1: function() { 18: // ... 19: }, 20: publicMethod2: function(args) { 21: // ... 22: } 23: } 24: } 25: return { 26: getInstance: function() { 27: if(!uniqueInstance) { // Instantiate only if the instance doesn't exist. 28: uniqueInstance = constructor(); 29: } 30: return uniqueInstance; 31: } 32: } 33: })(); 34: ? 35: //調用: 36: MyNamespace.Singleton.getInstance().publicMethod1();

?

JavaScript好書推薦(只推3本,須精讀)

? ?

總結

以上是生活随笔為你收集整理的JavaScript类的几种写法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。