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 ...
随机推荐
- 【转】Linux Posix Timer使用
原文网址:http://blog.csdn.net/hongszh/article/details/8608781 最强大的定时器接口来自POSIX时钟系列,其创建.初始化以及删除一个定时器的行动被分 ...
- TCP/IP详解学习笔记(14)-TCP连接的未来和性能(未写完)
在TCP刚出世的时候,其主要工作环境是以太网和SLIP之类的低速网络.随着高速网络的出现,让TCP协议的吞吐量更大,效率更高的要求就愈来愈迫切.为此,TCP增加了三个重要机制来对应现在的变化,他们是 ...
- hdu 3938 Portal(并查集+离线+kruskal)2011 Multi-University Training Contest 10
搜了题解才把题搞明白.明白之后发现其实题意很清晰,解题思路也很清晰,只是题目表述的很不清晰…… 大意如下—— 给你一个无向图,图中任意两点的距离是两点间所有路径上的某一条边,这条边需要满足两个条件:1 ...
- java web 学习十三(使用session防止表单重复提交)
在平时开发中,如果网速比较慢的情况下,用户提交表单后,发现服务器半天都没有响应,那么用户可能会以为是自己没有提交表单,就会再点击提交按钮重复提交表单,我们在开发中必须防止表单重复提交. 一.表单重复提 ...
- android左右滑动加载分页以及动态加载数据
android UI 往右滑动,滑动到最后一页就自动加载数据并显示 如图: package cn.anycall.ju; import java.util.ArrayList; import java ...
- csv文件与DataTable互相导入处理
封装处理下,以后项目用到可以直接使用,比较简单. 1.首先看封装好的类 using System; using System.Data; using System.IO; using System.T ...
- ASP.NET 中JSON 的序列化和反序列化
JSON是专门为浏览器中的网页上运行的JavaScript代码而设计的一种数据格式.在网站应用中使用JSON的场景越来越多,本文介绍ASP.NET中JSON的序列化和反序列化,主要对JSON的简单介绍 ...
- 关于如果修改 ie 浏览器 文本模式
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/html4/stric ...
- C++容器学习
以前自学C++的时候就没怎么看容器,一直以来也没怎么编过C++程序,现在想用C++写点东西,突感容器类型有些生疏,故做此笔记.(参考<C++ primer> 容器:容纳特定类型对象的集合. ...
- [Hive - LanguageManual ] Windowing and Analytics Functions (待)
LanguageManual WindowingAndAnalytics Skip to end of metadata Added by Lefty Leverenz, last edi ...