惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

The Last Watchdog
The Last Watchdog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
S
Secure Thoughts
MongoDB | Blog
MongoDB | Blog
博客园 - Franky
T
Tor Project blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Google DeepMind News
Google DeepMind News
L
LINUX DO - 最新话题
博客园_首页
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Vercel News
Vercel News
Last Week in AI
Last Week in AI
月光博客
月光博客
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
P
Proofpoint News Feed
博客园 - 叶小钗
NISL@THU
NISL@THU
C
Check Point Blog
K
Kaspersky official blog
N
News and Events Feed by Topic
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
A
Arctic Wolf
T
Threatpost
GbyAI
GbyAI
L
LINUX DO - 热门话题
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
P
Privacy & Cybersecurity Law Blog
N
News and Events Feed by Topic
Scott Helme
Scott Helme
P
Privacy International News Feed
The Register - Security
The Register - Security
G
GRAHAM CLULEY
Recorded Future
Recorded Future
Apple Machine Learning Research
Apple Machine Learning Research
C
Cybersecurity and Infrastructure Security Agency CISA
B
Blog
Project Zero
Project Zero
Cyberwarzone
Cyberwarzone
Webroot Blog
Webroot Blog
Microsoft Security Blog
Microsoft Security Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
D
DataBreaches.Net
J
Java Code Geeks
AWS News Blog
AWS News Blog
Help Net Security
Help Net Security
Engineering at Meta
Engineering at Meta
M
MIT News - Artificial intelligence
T
Threat Research - Cisco Blogs
Google DeepMind News
Google DeepMind News

博客园 - 曾伟

不干胶、热敏打印 wpf 滚屏数据显示 net core quartz调度 warp打包 nssm部署到windowsservice c# webapi 过滤器token、sign认证、访问日志 c# NPOI文件操作 c# ini文件操作 WCF多种调用方式兼容 网页打印javascript:window.print() window.showModalDialog弹出对话框刷新问题 ASP.NET] 选择文件夹的对话框 常用SQL语句书写技巧 控制同一exe程序打开多次 - 曾伟 - 博客园 IIS6 MMC检测到此管理单元发生一个错误,建议您关闭并重新启动mmc 在上传文件时限制上传文件的大小,并捕捉超过文件大小限制的 - 曾伟 - 博客园 Lucene的例子 javascript 获取标签具体位置 - 曾伟 - 博客园 Javascript实现截图功能(代码) 终端服务器超出了最大允许连接数 DBCC CHECKDB 数据库或表修复
JavaScript实现类,有多种方法。
曾伟 · 2010-03-30 · via 博客园 - 曾伟

方法一:构造方法。

  1. function coder(){
  2. this.name = '小王';
  3. this.job = '程序员';
  4. this.coding = function ()
  5. {
  6. alert('我正在写代码');
  7. }
  8. }

  9. var coder=new coder();
  10. alert(coder.name);
  11. coder.coding();

复制代码

方法二:工厂方法。

  1. function createCoderFactory(){
  2. var obj=new Object();
  3. obj.name = '小王';
  4. obj.job = '程序员';
  5. obj.coding = function (){
  6. alert('我正在写代码');
  7. };
  8. return obj;
  9. }
  10. var coder = createCoderFactory();
  11. alert(coder.name);
  12. coder.coding();

复制代码

但工厂方法和构造方法都有着一个相同的缺点,就是每创建一个实例,都会实例化该类的每个函数。

方法三:原形链。

  1. function coder(){}
  2. coder.prototype.name = '小王';
  3. coder.prototype.job = '程序员';
  4. coder.prototype.coding = function(){
  5. alert('我正在写代码');
  6. };
  7. var coder = new coder();
  8. alert(coder.name);
  9. coder.coding();

复制代码

但原行链有个缺点就是它所有属性都共享,只要一个实例改变其他的都会跟着改变。如:

  1. var coder1 = new coder();
  2. var coder2 = new coder();
  3. alert(coder1.name); /*显示“小王”*/
  4. coder2.name = '老王';
  5. alert(coder1.name); /*示“老王”*/
  6. alert(coder2.name); /*这个也显示“老王”*/

复制代码

方法四:混合方式。

以上三种都有着各自的缺点,所以我们要加以改进。

  1. function coder(){
  2. this.name = '小王';
  3. this.job = '程序员';
  4. }
  5. coder.prototype.coding = function(){
  6. alert('我正在写代码');
  7. };

复制代码

方法五:动态原链。

要解决前三种的缺点,还有一种方法。

  1. function coder(){
  2. this.name = '小王';
  3. this.job = '程序员';
  4. if (typeof(coder._init) == 'undefined'){
  5. this.coding = function ()
  6. {
  7. alert('我正在写代码');
  8. };
  9. this._init = true;
  10. }
  11. }