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的更多相关文章

  1. [Javascript] Introducing Reduce: Common Patterns

    Learn how two common array functions - map() and filter() - are syntactic sugar for reduce operation ...

  2. [Design Patterns] 01. Creational Patterns - Abstract Factory

    设计模式是一套被反复使用.多数人知晓的.经过分类编目的.代码设计经验的总结,使用设计模式的目的是提高代码的可重用性,让代码更容易被他人理解,并保证代码可靠性.它是代码编制真正实现工程化. 四个关键元素 ...

  3. [Design Patterns] 03. Behavioral Patterns - Observer Pattern

    前言 参考资源 Ref: 史上最全设计模式导学目录(完整版) 观察者模式-Observer Pattern[学习难度:★★★☆☆,使用频率:★★★★★] 对象间的联动——观察者模式(一):多人联机对战 ...

  4. [Design Patterns] 02. Structural Patterns - Facade Pattern

    前言 参考资源 史上最全设计模式导学目录(完整版) 只把常用的五星的掌握即可. 外观模式-Facade Pattern[学习难度:★☆☆☆☆,使用频率:★★★★★] 深入浅出外观模式(一):外观模式概 ...

  5. [Regular Expressions] Find Repeated Patterns

    Regular Expression Quantifiers allow us to identify a repeating sequence of characters of minimum an ...

  6. Architecture Patterns

    This chapter provides guidelines for using architecture patterns. Introduction Patterns for system a ...

  7. 【JavaScript】直接拿来用!最火的前端开源项目(一)

    摘要:对于开发者而言,了解当下比较流行的开源项目很是必要.利用这些项目,有时能够让你达到事半功倍的效果.为此,本文整理GitHub上最火的前端开源项目列表,这里按分类的方式列出前九个. 对于开发者而言 ...

  8. 每个JavaScript开发人员应该知道的33个概念

    每个JavaScript开发人员应该知道的33个概念 介绍 创建此存储库的目的是帮助开发人员在JavaScript中掌握他们的概念.这不是一项要求,而是未来研究的指南.它基于Stephen Curti ...

  9. SpingMVC 核心技术帮助文档

    声明:本篇文档主要是用于参考帮助文档,没有实例,但几乎包含了SpringMVC 4.2版本的所有核心技术,当前最新版本是4.3,4.2的版本已经经是很新的了,所以非常值得大家一读,对于读完这篇文档感觉 ...

随机推荐

  1. [Matlab.Matrix] reshape

    a=[1,2,3;4,5,6;7,8,9]; a=reshape(a,1,9); a=[1,4,7,2,5,8,3,6,9]; a=reshape(a,9,1); a=[1;4;7;2;5;8;3;6 ...

  2. Scrum 项目1.0 2.0 3.0 4.0 5.0 6.0 7.0

    1.确定选题. 应用NABCD模型,分析你们初步选定的项目,充分说明你们选题的理由. 录制为演说视频,上传到视频网站,并把链接发到团队博客上. 截止日期:2016.5.6日晚10点 阅读教材第8章,8 ...

  3. 【Win10】让 TextBlock 按字符换行

    要想将 TextBlock 里的文本自动换行的话,只需要设置 TextWrapping 属性为 Wrap 即可. 但是 TextWrapping 是尽可能根据空白字符来换行的,因此,就有可能出现下面这 ...

  4. ok6410 android driver(11)

    This essay, I go to a deeply studying to android HAL device driver program. According to the android ...

  5. MVC中Action之间传值

    一  MVCAction之间的传值 之前一直觉得关于MVC里面的传值,只能从<视图—>Action>,和<Actoin->视图>但是今天在项目里面需要实现将几个视图 ...

  6. [CLR via C#]25. 线程基础

    一.Windows为什么要支持线程 Microsoft设计OS内核时,他们决定在一个进程(process)中运行应用程序的每个实例.进程不过是应用程序的一个实例要使用的资源的一个集合.每个进程都赋予了 ...

  7. Ajax学习资源大全[本来是转载的,但是现在我增加了很多]

    本欲放转载区,但是这样一文章放那里基本是没有用的,帮助不了任何人!所以放新手了!! 我一般非经典或者自己用不上不转载,所以如果你不幸看见了的话,恰恰你又对AJAX有兴趣的话不防看下,也许对你有用的!! ...

  8. ActiveReports 报表控件官方中文入门教程 (2)-创建、数据源、浏览以及发布

    本篇文章将阐述首次使用 ActiveReports 报表控件 的方法,包括添加报表文件.绑定数据源以及如何发布报表等内容. ActiveReports 报表控件官方中文入门教程 (1)-安装.激活以及 ...

  9. [Eclipse] - 解决导入flask模块出现的Unresolved Import flask问题

    http://www.cnblogs.com/mizhon/p/4242073.html [Eclipse] - 解决导入flask模块出现的Unresolved Import flask问题 最近想 ...

  10. Web基础开发最核心要解决的问题

    Web基础开发要解决的问题,往往也就是那些框架出现的目的 - 要解决问题. 1. 便捷的Db操作: 2. 高效的表单处理: 3. 灵活的Url路由: 4. 合理的代码组织结构: 5. 架构延伸 缓存. ...