当前位置: 代码迷 >> JavaScript >> javascript定义类的3种实现方式
  详细解决方案

javascript定义类的3种实现方式

热度:616   发布时间:2013-12-07 23:44:44.0

1、使用函数定义

定义一个正常的JavaScript函数,这个应该是最普遍的一种方法,通过创建函数,给函数定一个函数名,然后在函数内定义我们所需要的属性或方法来,daimami.com 如下:

  1. function method (type) {

  2.    this.type = type;

  3.    this.color = "red";

  4.    this.getInfo = getMethodInfo;

  5. }

  6. function getMethodInfo() {

  7.    return this.color + ' ' + this.type + ' method ';

  8. }

然后我们现在需要实例化上面的函数,调用里面的方法和属性时,我们可以这样做:

  1. var newMethod = new method('animal');

  2. newMethod.color = "blue";

  3. alert(newMethod.getInfo());blue animal Method


1.1在函数内部定义方法:

   在上面的这种方法里面我们把我们的类方法单独写了一个函数来调用,这种方法的一个最大的缺点是增加了全局函数,容易发生全局函数污染,在大的项目里面,很容导致函数命名冲突,为了减少全局函数的污染,我们可以将函数方法,写在类里面,例如:

  1. function method (type) {

  2.    this.type = type;

  3.    this.color = "red";

  4.    this.getInfo = function(){

  5.        return this.color + ' ' + this.type + ' Method ';

  6.    };

  7. }

调用方法和上面是一样的:

  1. var newMethod = new method('animal');

  2. newMethod.color = "blue";

  3. alert(newMethod.getInfo());blue animal Method


1.2往函数添加原型方法,俗称工厂模式

1.1方法的缺点就是我们每次添加新的方法都需要到我们定义类里面去添加,这样子还是比较麻烦,也容易导致出错的,有没有什么办法能在不干预我们之前定义好的类的前提下,另外往这个类里面添加新的方法呢?这个时候我们就可以使用比较著名的工厂原型方法来实现,以上代码改写后:

  1. function method (type) {

  2.    this.type = type;

  3.    this.color = "red";

  4. }

  5. method.prototype.getInfo = function() {

  6.    return this.color + ' ' + this.type + ' Method';

  7. };

  8. method.prototype.getInfo2 = function() {

  9.    return this.color + ' ' + this.type + ' Method2';

  10. };

然后调用方法同上:

  1. var newMethod = new method('animal');

  2. newMethod.color = "blue";

  3. alert(newMethod.getInfo());//blue animal Method

  4. alert(newMethod.getInfo2());//blue animal Method2


2.对象字面量方法

对象字面量的方法非常的强大,在javascript中应用非常个广泛,其结构清晰,简单,下面我只做一个简单的介绍,有兴趣的同学建议可以看一看这篇文章《Using Objects to Organize Your Code》 。


我们平常定义一个数组的时候我们可能会用简写来代替,例如用:

  1. var arr = []

代替

  1. var arr = new Array();


当然我们创建对象的时候也可以用简写,例如:

  1. var fn = {}

来代替:

  1. var fn = new Object();


根据上面这个例子,我们也可以将我们上面的那个方法改写成对象字面量:

  1. var method = {

  2.    type: "animal",

  3.    color: "red",

  4.    getInfo: function () {

  5.        return this.color + ' ' + this.type + ' Method';

  6.    }

  7. }

通过上面这种方法有点是我们调用的时候就不需要再使用new来实例化,我们可以通过如下的方法来调用:

  1. method.color = "blue";

  2. alert(method.getInfo());//blue animal Method


上面这种方法我们有时候也称它为单例,因为他是不能在被实例化多个对象,我们只能单一的使用它。根据上的代码,我们如果用第一种方法来实现,是肯定会报错的:

  1. var newMethod = new Method(); //会报错


但是对象字面量法并不算是完全单例模式,我们其实可以通过深拷贝来继承他,相当将他的所有属性和方法拷贝给另一个对象来实现类似实例化的对象的方法。既然都说到这块了,我就简单说下深拷贝的实现方法吧:

深拷贝继承代码:

  1. function deepCopy(p, c) {

  2.    var c = c || {};

  3.   for (var i in p) {

  4.     if (typeof p[i] === 'object') {

  5.         c[i] = (p[i].constructor === Array) ? [] : {};

  6.       deepCopy(p[i], c[i]);

  7.     } else {

  8.       c[i] = p[i];

  9.     }

  10.   }

  11.   return c;

  12. }

然后我们通过上面这个方法,再将定一个对象来继承我们刚刚写的method方法:

  1. var method2 = deepCopy(method);

  2. method2.color = "blue";

  3. console.log(method2.getInfo());//blue animal Method

通过这种深拷贝,我们相当于是等于实例化了一个新的对象拥有了method的所有属性和方法,据说目前,jQuery库使用的就是这种继承方法,这个方法是我从别人那里看到的,并不是我发现的哈。


3.使用单例函数方法

第三种方法其实是结合了上面的两个方法。您可以使用一个函数来定义一个单例对象。例如你可以这样写:

  1. var method = new function() {

  2.    this.type = "animal";

  3.    this.color = "red";

  4.    this.getInfo = function () {

  5.        return this.color + ' ' + this.type + ' Method';

  6.    };

  7. }

上面的方法看起来似乎很像1.1的语法,但是我们调用上面的类的时候又是和对象字面量的方法一样:

  1. method.color = "blue";

  2. alert(method.getInfo());//blue animal Method


new function(){...}其实包含了两个功能:

  • 定义了一个函数(匿名函数)

  • 并实例化他后立即调用


上面这种方法比较少见,用法就看你自己怎么去选择了,如果你定义了一个类,但是只想使用一次,但你只会使用一次这个类,你可以通过上面的方法先给这个类一个名字,然后通过上的方法就可以只实例化一次了。


  相关解决方案