js类的几种写法

时间:2022-08-23 20:39:25

我们常用的有以下几种方法来用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&period;Struts2中Action类的三种写法

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

随机推荐

  1. VS替换空行

    visual studio2012 改变了正则表达式的写法 因此原来的不管用了 Old: ^:b*$\n New: ^(?([^\r\n])\s)*\r?$\r?\n Click Ctrl-H (qu ...

  2. VCC、VDD、VEE、VSS等有关电源标注的区别

    Almost all integrated circuits (ICs) have at least two pins which connect to the power rails of the ...

  3. Java 基础-反射

    反射-Reflect 测试用到的代码 1.接口 Person.java public interface Person { Boolean isMale = true; void say(); voi ...

  4. Winsock SPI-Socks5-SSL

  5. PyQt5 -pycharm 环境搭建

    1.安装PyQt5 在CMD窗口执行命令: pip3 install PyQt5 安装 pyqt_toools pip3 install PyQt5-tools 2.配置PyCharm 1)打开PyC ...

  6. hdu-2072(字典树)

    字典树模板题 代码 #include<iostream> #include<algorithm> #include<cstdio> #include<cstr ...

  7. 开源项目MultiChoiceAdapter详解(五)——可扩展的MultiChoiceBaseAdapter

    上次写到了开源项目MultiChoiceAdapter详解(四)——MultiChoiceBaseAdapter的使用,其实我们仍旧可以不使用ActionMode的,所以这里就写一个自己扩展的方法. ...

  8. 073——VUE中vuex之使用actions和axios异步初始购物车数据

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  9. 解析Java对象的equals&lpar;&rpar;和hashCode&lpar;&rpar;的使用

    解析Java对象的equals()和hashCode()的使用 前言 在Java语言中,equals()和hashCode()两个函数的使用是紧密配合的,你要是自己设计其中一个,就要设计另外一个.在多 ...

  10. Anaconda2

    Anaconda 是一个打包的python,一次把好多需要的包都安装好了.对于Python2.7把PyQt5都弄好了,不需要自己来编译! 看看这个 http://conda.pydata.org/do ...