我们常用的有以下几种方法来用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();

js类的几种写法的更多相关文章

  1. Python 自定义元类的两种写法

    有关元类是什么大家自己搜索了解,我这里写一下实现元类的两种写法 # 自定义元类 #继承type class LowercaseMeta(type): ''' 修改类的属性名称为小写的元类 ''' # ...

  2. [JS] 面向对象的5种写法和拓展JS对象的写法

    面向对象的JAVA  最开始当然是对象的定义了 收集了以下的五种写法 //第1种写法 function Circle(r) { this.r = r; } Circle.PI = 3.14159; C ...

  3. JS面向对象的几种写法

    JS 中,面向对象有几种写法.归纳下,大概有下面这几种:工厂模式,构造函数模式,原型模式,构造函数与原型模式的混合使用,原型链继承,借用构造函数继承. 一.工厂模式 function person ( ...

  4. js面向对象的五种写法

    第一种: //第1种写法 function Circle(r) { this.r = r; } Circle.PI = 3.14159; Circle.prototype.area = functio ...

  5. (转载)无缝滚动图片的js和jquery两种写法

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  6. JS 定时器的4种写法及介绍

    JS提供了一些原生方法来实现延时去执行某一段代码,下面来简单介绍一下setTiemout.setInterval.setImmediate.requestAnimationFrame. 一.什么是定时 ...

  7. js和jquery 两种写法 鼠标经过图片切换背景效果

    这个是javascript的写法 <img src="res/img/shop-c_32.jpg" alt="" onmouseover="th ...

  8. Struts2中Action类的三种写法

      一.普通的POJO类(没有继承没有实现)-基本不使用 POJO(Plain Ordinary Java Object)简单的Java对象,实际就是普通JavaBeans,是为了避免和EJB混淆所创 ...

  9. 3.Struts2中Action类的三种写法

    一.普通的POJO类(没有继承没有实现)-基本不使用 public class DemoAction1 { public String execute(){ System.out.println(&q ...

随机推荐

  1. android createbitmap函数内存溢出,求解怎样进行处理out of memory溢出问题

    android createbitmap函数内存溢出,求解怎样进行处理out of memory溢出问题 android createbitmap函数内存溢出,求解怎样进行处理out of memor ...

  2. Android 自己的自动化测试(4)&lt;uiautomator&gt;

    在前面的系列文章.我与介绍java实现 Android 自己主动化測试(1)怎样安装和卸载一个应用(java).Android 自己主动化測试(2)依据ID查找对象(java):然后又介绍了用pyth ...

  3. LR实战之Discuz开源论坛——登录脚本

    脚本业务流:访问Discuz论坛首页——登录论坛——退出论坛.本次使用LoadRunner11版本. 一.录制脚本注意 1.确保Discuz论坛能在服务器运行正常. 2.录制前先试访问Discuz论坛 ...

  4. 网页被Chrome识别成英语,区域,语言,网站

    修改成这个后解决 <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" ...

  5. HDU 5806 - NanoApe Loves Sequence Ⅱ (BestCoder Round #86)

    若 [i, j] 满足, 则 [i, j+1], [i, j+2]...[i,n]均满足 故设当前区间里个数为size, 对于每个 i ,找到刚满足 size == k 的 [i, j], ans + ...

  6. arm ldr 指令

    ldr 指令格式:(读取概念) ldr{条件} 1目的寄存器,2存储器地址 eg: ldr r0,[r1]; 把r1中数据值读取到r0中: ldr r0,[r1,r2];把r1+r2的数值 读取到r0 ...

  7. GDAL显示线性shp文件

    http://pan.baidu.com/s/1qWIDphU  (工程文件在vs2008中编写) 1.使用到的技术 GDAL:读取矢量数据 GDI:    绘制矢量数据 2.详细解释 GDI绘图: ...

  8. ( function(){…} )()

    javascript和其他编程语言相比比较随意,所以javascript代码中充满各种奇葩的写法,有时雾里看花,当然,能理解各型各色的写法也是对javascript语言特性更进一步的深入理解. ( f ...

  9. php的多线程使用

    PHP 5.3 以上版本,使用pthreads PHP扩展,可以使PHP真正地支持多线程.多线程在处理重复性的循环任务,能够大大缩短程序执行时间. 在liunx下的安装 准备工作: 1.下载Threa ...

  10. 配置阿里云作为yum 源

    第一步:下载aliyum 的yum源配置文件. http://mirrors.aliyun.com/repo/ 第二步:把下载到的repo文件复制到/etc/yum.repo.d/目录下. ----- ...