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
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· [AI/GPT/综述] AI Agent的设计模式综述