Javascript Design Patterns - Js Class
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的更多相关文章
- AMD - Learning JavaScript Design Patterns [Book] - O'Reilly
AMD - Learning JavaScript Design Patterns [Book] - O'Reilly The overall goal for the Asynchronous Mo ...
- Learning JavaScript Design Patterns The Module Pattern
The Module Pattern Modules Modules are an integral piece of any robust application's architecture an ...
- Learning JavaScript Design Patterns The Observer Pattern
The Observer Pattern The Observer is a design pattern where an object (known as a subject) maintains ...
- Learning JavaScript Design Patterns The Singleton Pattern
The Singleton Pattern The Singleton pattern is thus known because it restricts instantiation of a cl ...
- JavaScript Design Patterns: Mediator
The Mediator Design Pattern The Mediator is a behavioral design pattern in which objects, instead of ...
- Learning JavaScript Design Patterns The Constructor Pattern
In classical object-oriented programming languages, a constructor is a special method used to initia ...
- javascript design patterns
http://jsdesignpatterns.com/ http://www.joezimjs.com/tag/design-patterns/ http://codecube.net/#archi ...
- Design Patterns All in One (JavaScript Version)
Design Patterns All in One (JavaScript Version) JavaScript 设计模式 JavaScript 数据结构 23种设计模式分为 3 大类: 创建型模 ...
- javascript / PHP [Design Patterns - Facade Pattern]
This pattern involves a single class which provides simplified methods required by client and delega ...
随机推荐
- Delphi DecodeDate和EncodeDate 拆分和聚合时间函数的用法
SysUtilsprocedure DecodeData(Date: TDateTime; var Year, Month, Day: Word);DecodeDate打断TdateTime成为年月日 ...
- UVALive 4255 Guess
这题竟然是图论···orz 题意:给出一个整数序列a1,a2,--,可以得到如下矩阵 1 2 3 4 1 - + 0 + 2 + + + 3 - - 4 + &quo ...
- Oracle数据库中的违规策略规则的修正
如笔者计算机上违规的策略与规则: 为了安全,可如下方式对齐进行修正:
- 基于Fragment实现Tab的切换,滑出侧边栏
最近在学习Fragment(碎片)这是android3.0以后提出的概念,很多pad上面的设置部分都是通过Fragment来实现的,先看看具体的效果吧(图一) (图二) (图三)第一章图片是初始时的 ...
- 【转】ASP.NET网站怎么发布web项目程序和怎么部署
如何发布: http://jingyan.baidu.com/article/ca00d56c7303ffe99eebcfb0.html windows7 iis安装与配置 如何部署: http:// ...
- 【LR】录制测试脚本中的基本菜单
学习来源: MBoo,小强老师性能测试及Loadrunner培训 ——录制测试脚本: 1.Vuser -> run-time settings ->General Run Logic : ...
- 将iOS中Safari 的默认搜索引擎由google.cn改为google.com的方法
众所周知虽然Google大部分的业务已经迁出中国大陆,访问Google的中国站点只会出现一个投影网站,但是很长一段时间里如果想要访问Google仍然能跳转到google.com.hk这个香港的节点,这 ...
- 利用Spring.Net技术打造可切换的分布式缓存读写类
利用Spring.Net技术打造可切换的Memcached分布式缓存读写类 Memcached是一个高性能的分布式内存对象缓存系统,因为工作在内存,读写速率比数据库高的不是一般的多,和Radis一样具 ...
- mvc bundle功能(2)
配置好Bundle,注册好之后,再是调用 <head> <meta charset="utf-8"> <meta http-equiv="X ...
- vs2012不能打开项目解决办法
只要卸载这两个不定就能解决问题.