js类的几种写法
我们常用的有以下几种方法来用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类的几种写法的更多相关文章
- Python 自定义元类的两种写法
有关元类是什么大家自己搜索了解,我这里写一下实现元类的两种写法 # 自定义元类 #继承type class LowercaseMeta(type): ''' 修改类的属性名称为小写的元类 ''' # ...
- [JS] 面向对象的5种写法和拓展JS对象的写法
面向对象的JAVA 最开始当然是对象的定义了 收集了以下的五种写法 //第1种写法 function Circle(r) { this.r = r; } Circle.PI = 3.14159; C ...
- JS面向对象的几种写法
JS 中,面向对象有几种写法.归纳下,大概有下面这几种:工厂模式,构造函数模式,原型模式,构造函数与原型模式的混合使用,原型链继承,借用构造函数继承. 一.工厂模式 function person ( ...
- js面向对象的五种写法
第一种: //第1种写法 function Circle(r) { this.r = r; } Circle.PI = 3.14159; Circle.prototype.area = functio ...
- (转载)无缝滚动图片的js和jquery两种写法
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- JS 定时器的4种写法及介绍
JS提供了一些原生方法来实现延时去执行某一段代码,下面来简单介绍一下setTiemout.setInterval.setImmediate.requestAnimationFrame. 一.什么是定时 ...
- js和jquery 两种写法 鼠标经过图片切换背景效果
这个是javascript的写法 <img src="res/img/shop-c_32.jpg" alt="" onmouseover="th ...
- Struts2中Action类的三种写法
一.普通的POJO类(没有继承没有实现)-基本不使用 POJO(Plain Ordinary Java Object)简单的Java对象,实际就是普通JavaBeans,是为了避免和EJB混淆所创 ...
- 3.Struts2中Action类的三种写法
一.普通的POJO类(没有继承没有实现)-基本不使用 public class DemoAction1 { public String execute(){ System.out.println(&q ...
随机推荐
- HTML标签的offset、client、 scroll和currentStyle属性
本文来自:http://www.cnblogs.com/quanhai/archive/2010/04/19/1715231.html offsetHeight = borderTopWidth + ...
- 关于MAC的pkg和mpkg的分别
程序制作完毕后,在mac下通常的方法是要制作一个pkg的安装包,可是你会发现pkg和mpkg的文件出现的比較多,笔者也是经过了一定的试验和尝试,才了解到,pkg是单个文件的pkg,而mpkg事实上是多 ...
- RMAN数据库恢复之恢复归档日志文件
恢复归档日志文件如果只是为了在恢复数据文件之后应用归档文件,那并不需要手动对归档文件进行恢复,RMAN会在RECOVER时自动对适当的归档进行恢复.单独恢复归档文件一般是有特别的需求,如创建了Data ...
- heroku
一个可以在上面部署自己的应用一个共享平台,例如可以部署访问google的代理 地址为:https://www.heroku.com/
- iOS基本内存管理:autorelease和autoreleasepool
1.autorelease 基本用法 对象执行autorelease方法时会将对象添加到自动释放池中 当自动释放池销毁时自动释放池中所有对象作release操作 对象执行autorelease方法后自 ...
- 005 列表以及append,extend方法
定义一个列表: number=[,'changhao','常浩',5.2] 往这个列表里面添加单一新值(类型无限制),需要使用append方法. 例如: number.append() number. ...
- Spring学习之Ioc控制反转(1)
开始之前: 1. 本博文为原创,转载请注明出处 2. 作者非计算机科班出身,如有错误,请多指正 ---------------------------------------------------- ...
- 从汇编看c++中指向成员变量的指针(一)
在c++中,指向类成员变量的指针存储的并不是该成员变量所在内存的地址,而仅仅是该成员变量在该类对象中相对于对象首地址的偏移量.因此,它必须绑定到某一个对象或者对象指针上面,这里的对象和对象指针,就相当 ...
- ckeditor 使用手册
CKEditor使用手册 在使用CKEditor过程中遇到了一些问题,现把它整理成手册,以便随时翻阅. 在页面<head>中引入ckeditor核心文件ckeditor.js <sc ...
- java 小数点取2位并且四舍五入
BigDecimal bd = new BigDecimal(0.0199999999999); System.out.println("res:"+bd.setScale(2, ...