When you invoke the constructor function with new, the following happens inside the function:

• An empty object is created and referenced by this variable, inheriting the prototype of the function.

• Properties and methods are added to the object referenced by this.

• The newly created object referenced by this is returned at the end implicitly (if no other object was returned explicitly).

var Person = function (name) {

    this.name = name;

    this.say = function () {

        return "I am " + this.name;

    };

};   

var adam = new Person("Adam");

adam.say(); // "I am Adam" 

Note

reusable members, such as methods, should go to the prototype.

Person.prototype.say = function () {

    return "I am " + this.name;

};

Constructor's Return Values

When invoked with new, a constructor function always returns an object inheriting from the constructor's prototype.

var Objectmaker = function () {

    // this `name` property will be ignored

    // because the constructor

    // decides to return another object instead

    this.name = "This is it";

    // creating and returning a new object

    var that = {};

    that.name = "And that's that";

    return that;

};

// test

var o = new Objectmaker();

console.log(o.name); // "And that's that"   

You have the freedom to return any object in your constructors, as long as it's an object. Attempting to return something that's not an object (like a string or a boolean false, for example) will not cause an error but will simply be ignored, and the object referenced by this will be returned instead.

JavaScript Patterns 3.2 Custom Constructor Functions的更多相关文章

  1. JavaScript Patterns 7.1 Singleton

    7.1 Singleton The idea of the singleton pattern is to have only one instance of a specific class. Th ...

  2. JavaScript Patterns 5.5 Sandbox Pattern

    Drawbacks of the namespacing pattern • Reliance on a single global variable to be the application’s ...

  3. JavaScript Patterns 5.3 Private Properties and Methods

    All object members are public in JavaScript. var myobj = { myprop : 1, getProp : function() { return ...

  4. JavaScript Patterns 6.5 Inheritance by Copying Properties

    Shallow copy pattern function extend(parent, child) { var i; child = child || {}; for (i in parent) ...

  5. JavaScript Patterns 6.4 Prototypal Inheritance

    No classes involved; Objects inherit from other objects. Use an empty temporary constructor function ...

  6. JavaScript Patterns 6.3 Klass

    Commonalities • There’s a convention on how to name a method, which is to be considered the construc ...

  7. JavaScript Patterns 6.2 Expected Outcome When Using Classical Inheritance

    // the parent constructor function Parent(name) { this.name = name || 'Adam'; } // adding functional ...

  8. JavaScript Patterns 6.1 Classical Versus Modern Inheritance Patterns

    In Java you could do something like: Person adam = new Person(); In JavaScript you would do: var ada ...

  9. JavaScript Patterns 5.9 method() Method

    Advantage Avoid re-created instance method to this inside of the constructor. method() implementatio ...

随机推荐

  1. OpenCV开发环境配置-Windows/MinGW/Clion/CMake

    临时更换成了TDM-GCC,和mingw类似,这里只是声明一下. 由于opencv下载下来的.exe安装包实际上是没有mingw(gcc)匹配的/动静态库,因此这些东西需要我们自己使用mingw编译. ...

  2. Qt之QAbstractItemView右键菜单

    一.功能概述 说起右键菜单,之前Qt之自定义QLineEdit右键菜单这篇文章中我已经讲述过3种右键菜单的实现方式,今儿也是在啰嗦一下,针对QListWidget类在定制一下右键菜单,我使用的具体方式 ...

  3. iOS 9.2新增API

    CloudKit 新增CKFetchWebAuthTokenOperation类 CKFetchWebAuthTokenOperation对象从使用指定的cloudkit中的APIToken获取一个w ...

  4. SpringMVC基础——@ModelAttribute和@SessionAttribute

    一.@ModelAttribute 注解 对方法标注 @ModelAttribute 注解,在调用各个目标方法前都会去调用 @ModelAttribute 标记的注解.本质上来说,允许我们在调用目标方 ...

  5. Scrum 1.0

    1.确定选题. 应用NABCD模型,分析你们初步选定的项目,充分说明你们选题的理由. 录制为演说视频,上传到视频网站,并把链接发到团队博客上. 项目:一个售书网站(O2O) 下面是NABCD模型: 1 ...

  6. 利用IronJs在.NET程序里面跑javascript脚本

    what’s dlr The dynamic language runtime (DLR) is a runtime environment that adds a set of services f ...

  7. 在SQL存储过程中给条件变量加上单引号

    在SQL存储过程中给条件变量加上单引号,不加语句就会出问题,以下就是在存储过程中将条件where设置成了动态变化的,给where赋完值再和前面的语句拼接,再execute(SQL) ), )), )+ ...

  8. hibernate4整合spring3事务问题

    本文是作者在对hibernate4+spring3+struts2整合中遇到的一个问题.对s2sh进行了基本的整合搭建以后,就是对事务的控制管理,将hibernate的事务交由spring管理.根据网 ...

  9. Oracle不足与MySQL优势

    Oracle库主要不足:1.单点故障:2.付费License;3.不支持水平扩展. MySQL及水平拆库的优势:1.单点故障影响率1/N:2.免费:3.可低成本水平扩展,近乎无限的水平扩展能力.

  10. [Tool] 使用Astah绘制UML图形

    [Tool] 使用Astah绘制UML图形 前言 在软件开发的过程中,开发人员可以绘制UML图形来将分析设计内容转化为图形化文件,方便在团队之间传递分析设计结果.但在团队经费有限的情景中,可能没办法为 ...