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

推荐订阅源

D
DataBreaches.Net
SecWiki News
SecWiki News
博客园_首页
人人都是产品经理
人人都是产品经理
博客园 - 聂微东
P
Palo Alto Networks Blog
V
Vulnerabilities – Threatpost
Project Zero
Project Zero
WordPress大学
WordPress大学
NISL@THU
NISL@THU
酷 壳 – CoolShell
酷 壳 – CoolShell
P
Privacy & Cybersecurity Law Blog
Jina AI
Jina AI
AWS News Blog
AWS News Blog
Scott Helme
Scott Helme
Martin Fowler
Martin Fowler
C
Cybersecurity and Infrastructure Security Agency CISA
Forbes - Security
Forbes - Security
H
Heimdal Security Blog
小众软件
小众软件
I
Intezer
A
Arctic Wolf
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
O
OpenAI News
S
Security Affairs
阮一峰的网络日志
阮一峰的网络日志
Latest news
Latest news
G
GRAHAM CLULEY
Blog — PlanetScale
Blog — PlanetScale
J
Java Code Geeks
N
News and Events Feed by Topic
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
V2EX - 技术
V2EX - 技术
Stack Overflow Blog
Stack Overflow Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
L
LINUX DO - 最新话题
博客园 - Franky
P
Proofpoint News Feed
aimingoo的专栏
aimingoo的专栏
博客园 - 司徒正美
P
Proofpoint News Feed
S
Secure Thoughts
Google DeepMind News
Google DeepMind News
Microsoft Security Blog
Microsoft Security Blog
T
The Exploit Database - CXSecurity.com
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
C
CXSECURITY Database RSS Feed - CXSecurity.com
F
Full Disclosure
Security Latest
Security Latest

博客园 - 曾伟

不干胶、热敏打印 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. }