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 ...
随机推荐
- c++引擎开发
MyMap.erase(Itor++); //在windows下也可以Itor = MyMap.erase(Itor),但是在linux下不行. 一个是把指针定为const .就是不能修改指针.也就是 ...
- Linux 下mysql修改数据库存放目录方法和可能遇到的问题
MySQL版本:5.6.23-enterprise-commercial-advanced ,使用rpm安装linux:Red Hat Enterprise Linux Server release ...
- zookeeper 丢失事件/miss event
今天在统计页面上发现有个节点丢失了,经过仔细分析后,发现同一个节点上的二个应用(同时监控zk)其中一个丢失了一个event,检查zk cluster没有发现异常... 通过网络搜寻,出现miss ev ...
- android 根据网络来获取外网ip地址及国家,地区的接口
新浪的IP地址查询接口:http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js 新浪多地域测试方法:http://int.dpool. ...
- es3中使用es6/7的字符串扩展
最近在看阮一峰的<ES6标准入门>,在字符串扩展一节中有提到几个新的扩展,觉得挺有意思,想在ES3里面使用,于是就有下面的兼容性写法. repeat 将一个字符串重复n次 String.p ...
- 【Android】设备标识简介(imei imsi mac地址)
IMEI: 1- 意义: 参考http://zh.wikipedia.org/zh-cn/IMEI 国际移动设备辨识码 ,共15位,和厂商,产地等有关. 2- 获取: 直接查看设备信息,设置-关于手 ...
- 去除除服串中的某些字符,不用String内置方法
import java.util.regex.Matcher; import java.util.regex.Pattern; public class test { public static vo ...
- yii中使用active record进行关联显示
model中: view中:
- .NET4.0下使用Net2.0类库或程序集
最近在项目上一直使用.net4.0 framework,使用ffmepeg下的一个dll时,提示只能在2.0下运行,解决方法如下: app.config中添加一个配置节:startup <?xm ...
- ZJUTACM(hd1259)
ZJUTACM 点我 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...