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. php的SAPI,CLI SAPI,CGI SAPI

    首先一个问题:在命令行下执行:php -r 'echo 12;' 控制台会打印出 12: 这个过程不是很奇妙么,我输入的是shell命令,但是执行的却是php脚本.php脚本执行完成之后的输出还能在控 ...

  2. Java魔法堂:注解用法详解——@SuppressWarnings

    一.前言 编码时我们总会发现如下变量未被使用的警告提示: 上述代码编译通过且可以运行,但每行前面的“感叹号”就严重阻碍了我们判断该行是否设置的断点了.这时我们可以在方法前添加 @SuppressWar ...

  3. SQL年月日方面的查询信息

    这是计算一个月第一天的SQL 脚本:   SELECT DATEADD(mm, DATEDIFF(mm,0,getdate()), 0) --当月的第一天 SELECT DATEADD(mm, DAT ...

  4. sql分页存储过程

    ALTER PROCEDURE [dbo].[P_SplitPagesQuery] @TablesName NVARCHAR(MAX),--表名或视图名(只能传单一表名) @PK NVARCHAR(M ...

  5. jquery ajax给外部变量赋值 async: false

    开发过程中用到检查是否存在手机号问题. //验证手机号是否注册            var bl = false;            $.ajax({                type: ...

  6. 重新想象 Windows 8 Store Apps (57) - 本地化和全球化

    [源码下载] 重新想象 Windows 8 Store Apps (57) - 本地化和全球化 作者:webabcd 介绍重新想象 Windows 8 Store Apps 之 本地化和全球化 本地化 ...

  7. 小型app开发的思路

    前提: 1. 性能不是最重要: 2. 人手少: 3. 速度要快: 结论: 1. 混合式 2. 减少app的复杂程度 3. 追求性能 (博客,尽量让自己每天写一点,短一点都可以)

  8. AC自动机---病毒侵袭

    HDU 2896 题目网址: http://acm.hust.edu.cn/vjudge/contest/view.action?cid=110773#problem/B Description 当太 ...

  9. 泛函编程(9)-异常处理-Option

    Option是一种新的数据类型.形象的来描述:Option就是一种特殊的List,都是把数据放在一个管子里:然后在管子内部对数据进行各种操作.所以Option的数据操作与List很相似.不同的是Opt ...

  10. A -- HDU 4585 Shaolin

    Shaolin Time Limit: 1000 MS Memory Limit: 32768 KB 64-bit integer IO format: %I64d , %I64u Java clas ...