JavaScript Patterns 3.3 Patterns for Enforcing new
When your constructor has something like this.member and you invoke the constructor without new, you’re actually creating a new property of the global object called member and accessible through window.member or simply member.
// constructor
function Waffle() {
this.tastes = "yummy";
}
// a new object
var good_morning = new Waffle();
console.log(typeof good_morning); // "object"
console.log(good_morning.tastes); // "yummy"
// antipattern:
// forgotten `new`
var good_morning = Waffle();
console.log(typeof good_morning); // "undefined"
console.log(window.tastes); // "yummy"
This undesired behavior is addressed in ECMAScript 5, and in strict mode this will no longer point to the global object. If ES5 is not available, there’s still something you can do to make sure that a constructor function always behaves like one even if called without new.
Naming Convention
uppercase the first letter in constructor names (MyConstructor) and lowercase it in “normal” functions and methods (myFunction).
Using that
function Waffle() {
var that = {};
that.tastes = "yummy";
return that;
}
function Waffle() {
return {
tastes: "yummy"
};
}
var first = new Waffle(),
second = Waffle();
console.log(first.tastes); // "yummy"
console.log(second.tastes); // "yummy"
Disadvantage
The link to the prototype is lost, so any members you add to the Waffle()prototype will not be available to the objects.
Self-Invoking Constructor
In the constructor you check whether this is an instance of your constructor, and if not, the constructor invokes itself again, this time properly with new:
function Waffle() {
if (!(this instanceof Waffle)) {
return new Waffle();
}
this.tastes = "yummy";
}
Waffle.prototype.wantAnother = true;
// testing invocations
var first = new Waffle(),
second = Waffle();
console.log(first.tastes); // "yummy"
console.log(second.tastes); // "yummy"
console.log(first.wantAnother); // true
console.log(second.wantAnother); // true
JavaScript Patterns 3.3 Patterns for Enforcing new的更多相关文章
- [Javascript] Introducing Reduce: Common Patterns
Learn how two common array functions - map() and filter() - are syntactic sugar for reduce operation ...
- [Design Patterns] 01. Creational Patterns - Abstract Factory
设计模式是一套被反复使用.多数人知晓的.经过分类编目的.代码设计经验的总结,使用设计模式的目的是提高代码的可重用性,让代码更容易被他人理解,并保证代码可靠性.它是代码编制真正实现工程化. 四个关键元素 ...
- [Design Patterns] 03. Behavioral Patterns - Observer Pattern
前言 参考资源 Ref: 史上最全设计模式导学目录(完整版) 观察者模式-Observer Pattern[学习难度:★★★☆☆,使用频率:★★★★★] 对象间的联动——观察者模式(一):多人联机对战 ...
- [Design Patterns] 02. Structural Patterns - Facade Pattern
前言 参考资源 史上最全设计模式导学目录(完整版) 只把常用的五星的掌握即可. 外观模式-Facade Pattern[学习难度:★☆☆☆☆,使用频率:★★★★★] 深入浅出外观模式(一):外观模式概 ...
- [Regular Expressions] Find Repeated Patterns
Regular Expression Quantifiers allow us to identify a repeating sequence of characters of minimum an ...
- Architecture Patterns
This chapter provides guidelines for using architecture patterns. Introduction Patterns for system a ...
- 【JavaScript】直接拿来用!最火的前端开源项目(一)
摘要:对于开发者而言,了解当下比较流行的开源项目很是必要.利用这些项目,有时能够让你达到事半功倍的效果.为此,本文整理GitHub上最火的前端开源项目列表,这里按分类的方式列出前九个. 对于开发者而言 ...
- 每个JavaScript开发人员应该知道的33个概念
每个JavaScript开发人员应该知道的33个概念 介绍 创建此存储库的目的是帮助开发人员在JavaScript中掌握他们的概念.这不是一项要求,而是未来研究的指南.它基于Stephen Curti ...
- SpingMVC 核心技术帮助文档
声明:本篇文档主要是用于参考帮助文档,没有实例,但几乎包含了SpringMVC 4.2版本的所有核心技术,当前最新版本是4.3,4.2的版本已经经是很新的了,所以非常值得大家一读,对于读完这篇文档感觉 ...
随机推荐
- 性能测试类,让你写法代码养成经常测试的好习惯 -ASP.NET C#
介绍: 可以很方便的在代码里循环执行 需要测试的函数 自动统计出执行时间,支持多线程. 使用方法: PerformanceTest p = new PerformanceTest(); p.SetC ...
- PP66 EEPPPPMM SSyysstteemm AAddmmiinniissttrraattiioonn GGuuiiddee 16 R1
※★◆●PP66 EEPPPPMM SSyysstteemm AAddmmiinniissttrraattiioonn GGuuiiddee 16 R1AApprriill 22001166Conte ...
- RSA密钥——JAVA与C#的区别和联系
PS:好久没写博了,最近在考虑以后的事情,而且手上杂事也比较多,终于得空来写两篇. 首先感谢:http://www.codeproject.com/Articles/25487/Cryptogra ...
- 与众不同 windows phone (42) - 8.0 相机和照片: 通过 PhotoCaptureDevice 捕获照片
[源码下载] 与众不同 windows phone (42) - 8.0 相机和照片: 通过 PhotoCaptureDevice 捕获照片 作者:webabcd 介绍与众不同 windows pho ...
- SignalR+Asp.net高频率实时消息传递应用
1.概述: 使用 ASP.NET 和SignalR 2高频率的实时消息功能.高频率消息在这种情况下就意味着更新发送以固定的速率; 本教程中创建的应用程序显示一个用户可以拖动的形状.在所有其他连接浏览器 ...
- java之Class类详解
测试中需要用到的代码 InterfaceA代码: package jichu; interface InterfaceA { String s1 = "this is s1 in Inter ...
- EffectiveJava——类层次优于标签类
标签类: 有时候,可能会遇到带有两种甚至更多钟风格的类的实例的类,并包含表示实例风格的(tag)域.例如下面这个类,它能够表示圆形或者矩形: /** * 类层次优先与标签类 * @author wei ...
- html格式化
解决方法是: 在myeclipse中是这样解决的: 点击 myeclipse菜单栏的 window选项卡,找到下拉 perferences 选项 , 在里面快捷 "搜索" 框里面输 ...
- mvc5入门示例博客(有惊喜)
因为一直做pc客户端,总感觉要被社会淘汰一样,近来时间又有空闲,索性学习一下asp.net mvc开发,试着追赶互联网的潮流. 话说在软件开发中,最费力的还是界面上,太多细节要关注了,从今年起便努力将 ...
- mysql存储过程性能监控和分析
公司当前版本的系统大量的使用了存储过程,有些复杂的过程套过程,一个主调用者可能最多调用其它几十个小的业务逻辑和判断,不要说这么做很不合理,在大陆,目前至少30%的证券交易系统代码都是用存储过程写业务逻 ...