JavaScript is a class-less language, however classes can be simulated using functions.

eg:

// A car 'class'
function Car(model) {
this.model = model;
this.color = 'silver';
this.year = '2012';
this.getInfo = function () {
return this.model + ' ' + this.year;
}
}

We can then instantiate the object using the Car constructor we defined above like this:

var myCar = new Car('ford');
myCar.year = '2010';
console.log(myCar.getInfo());

另一个例子

var Person = function (name) {
this.name = name;
this.say = function () {
return "I am " + this.name;
};
}; // use the class
var adam = new Person("Adam");
adam.say(); // "I am Adam"

上述关于类函数的做法性能不佳,原因如下:

any time you call new Person() a new function is created in memory.

This is obviously inefficient, because the say() method doesn’t change from one instance to the next.

(每次new对象的时候会在内存中新建一个函数)

The better option is to add the method to the prototype of Person 

(比较好的方法是把方法增加到Person对象的prototype)

Person.prototype.say = function () {
return "I am " + this.name;
};

just remember that reusable members, such as methods, should go to the prototype 

(关于JS类必须要记住的是,可重用的成员,例如方法必须放到对象的prototype)

Javascript Design Patterns - Js Class的更多相关文章

  1. AMD - Learning JavaScript Design Patterns [Book] - O'Reilly

    AMD - Learning JavaScript Design Patterns [Book] - O'Reilly The overall goal for the Asynchronous Mo ...

  2. Learning JavaScript Design Patterns The Module Pattern

    The Module Pattern Modules Modules are an integral piece of any robust application's architecture an ...

  3. Learning JavaScript Design Patterns The Observer Pattern

    The Observer Pattern The Observer is a design pattern where an object (known as a subject) maintains ...

  4. Learning JavaScript Design Patterns The Singleton Pattern

    The Singleton Pattern The Singleton pattern is thus known because it restricts instantiation of a cl ...

  5. JavaScript Design Patterns: Mediator

    The Mediator Design Pattern The Mediator is a behavioral design pattern in which objects, instead of ...

  6. Learning JavaScript Design Patterns The Constructor Pattern

    In classical object-oriented programming languages, a constructor is a special method used to initia ...

  7. javascript design patterns

    http://jsdesignpatterns.com/ http://www.joezimjs.com/tag/design-patterns/ http://codecube.net/#archi ...

  8. Design Patterns All in One (JavaScript Version)

    Design Patterns All in One (JavaScript Version) JavaScript 设计模式 JavaScript 数据结构 23种设计模式分为 3 大类: 创建型模 ...

  9. javascript / PHP [Design Patterns - Facade Pattern]

    This pattern involves a single class which provides simplified methods required by client and delega ...

随机推荐

  1. Oracle 课程二之Oracle数据库逻辑结构

    完成本课程的学习后,您应该能够: •数据库总体的逻辑结构 •深入理解数据库最小存储单元block •理解行迁移和行链接 •理解高水位线 •Drop.truncate.delete区别   1.数据库的 ...

  2. 关于UT的一些总结

    本文是个人对于UT的一些想法和总结,参考时建议请查阅官方资料. 转载请注明出处:http://www.cnblogs.com/sizzle/p/4476392.html 测试思想 编写UT测试代码,通 ...

  3. CF 577B Modulo Sum

    题意:给一个长度为n的正整数序列,问能不能找到一个不连续的子序列的和可以被m整除. 解法:抽屉原理+dp.首先当m<n时一定是有答案的,因为根据抽屉原理,当得到这个序列的n个前缀和%m时,一定会 ...

  4. POJ 1173 Find them, Catch them

    题意:有两个帮派,每个人只属于一个帮派,m次操作,一种操作告诉你两个人不是一个帮派的,另一种操作问两个人是不是在一个帮派. 解法:并查集+向量偏移.偏移量表示和根节点是不是同一帮派,是为0,不是为1. ...

  5. delphi 设置表格样式。

    //设置表格样式 wordDoc.Tables.Item(1).Borders.Item(Word.WdBorderType.wdBorderLeft).LineStyle = Word.WdLine ...

  6. HDU 5001-Walk(概率dp)

    题意: 给你一个图,求在长度为d的所有路径,不经过每个结点的概率 分析: 枚举每个结点,正推求概率 #include <map> #include <set> #include ...

  7. HDU 4630-No Pain No Game(线段树+离线处理)

    题意: 给你n个数的序列a,q个询问,每个询问给l,r,求在下标i在[l,r]的区间任意两个数的最大公约数中的最大值 分析: 有了hdu3333经验,我们从左向右扫序列,如果当前数的约数在前面出现过, ...

  8. UITextView 不左上角显示

    在Autolayout中 UITextView显示不左上角显示,修改如下 在viewDidLoad里面添加如下代码 if([[[UIDevice currentDevice] systemVersio ...

  9. SSH超时断开(ClientAliveInterval和ClientAliveCountMax )的使用

    有 2个方法 1 配置服务器 打开 /etc/ssh/sshd_config 找到 ClientAliveInterval 参数,如果没有就自己加一行 数值是秒,比如你设置为300,就是5分钟. Cl ...

  10. 《Python 学习手册4th》 第四章 介绍Python对象类型

    ''' 时间: 9月5日 - 9月30日 要求: 1. 书本内容总结归纳,整理在博客园笔记上传 2. 完成所有课后习题 注:“#” 后加的是备注内容(每天看42页内容,可以保证月底看完此书) ''' ...