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 ...
随机推荐
- MVC中前台所得
前台页面时间格式修改: @item.CreateTime.ToString("yyyy-MM-dd hh:mm:ss") 前台方法调用传参数: <a href="# ...
- Arrays.asList引起的惨案
最近代码中需要对两个数组求交,想当然便用到了List中的retainAll函数,但要将将数组转换成list.代码如下: String[] abc = new String[] { "abc& ...
- C语言内存地址基础
来源:http://blog.jobbole.com/44845/ 从计算机内存的角度思考C语言中的一切东东,是挺有帮助的.我们可以把计算机内存想象成一个字节数组,内存中每一个地址表示 1 字节.比方 ...
- IBM笔试题(_与equals的区别)
http://topic.csdn.net/t/20050325/08/3879427.html 题目:Integer i = new Integer(42) Long l ...
- 11、WebView 使用总结
<WebView android:id="@+id/webview" android:background="@color/white" android: ...
- codeforces 687D Dividing Kingdom II 带权并查集(dsu)
题意:给你m条边,每条边有一个权值,每次询问只保留编号l到r的边,让你把这个图分成两部分 一个方案的耗费是当前符合条件的边的最大权值(符合条件的边指两段点都在一个部分),问你如何分,可以让耗费最小 分 ...
- PHP 正则表达式总结
可以用字符作为一个通配符来代替除换行符(\n)之外的任一个字符.例如,正则表达式:.at可以与"cat"."sat"."#at"和" ...
- 导入CSV文件之后出现换行符问题
在用sqlldr导入数据之后,出现数据无法匹配的情况 在用plsql点击的时候,发现出现换行符的情况,从而使用下面的方法进行匹配 select q.comments from q where repl ...
- TP分析
http://blog.csdn.net/l627859442/article/details/7633457 http://blog.chinaunix.net/uid-27717694-id-37 ...
- wuzhicms内的全局函数--load_class()
load_class() 可以加载并实例化/coreframe/app/模块名/libs/class/$class.class.php类文件里的对象,如果有扩展类文件EXT_$class.class. ...