JavaScript Patterns 3.2 Custom Constructor Functions
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的更多相关文章
- 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 ...
- JavaScript Patterns 5.5 Sandbox Pattern
Drawbacks of the namespacing pattern • Reliance on a single global variable to be the application’s ...
- JavaScript Patterns 5.3 Private Properties and Methods
All object members are public in JavaScript. var myobj = { myprop : 1, getProp : function() { return ...
- JavaScript Patterns 6.5 Inheritance by Copying Properties
Shallow copy pattern function extend(parent, child) { var i; child = child || {}; for (i in parent) ...
- JavaScript Patterns 6.4 Prototypal Inheritance
No classes involved; Objects inherit from other objects. Use an empty temporary constructor function ...
- JavaScript Patterns 6.3 Klass
Commonalities • There’s a convention on how to name a method, which is to be considered the construc ...
- JavaScript Patterns 6.2 Expected Outcome When Using Classical Inheritance
// the parent constructor function Parent(name) { this.name = name || 'Adam'; } // adding functional ...
- 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 ...
- JavaScript Patterns 5.9 method() Method
Advantage Avoid re-created instance method to this inside of the constructor. method() implementatio ...
随机推荐
- Cocos2dx-3.0版本 从开发环境搭建(Win32)到项目移植Android平台过程详解
作为重量级的跨平台开发的游戏引擎,Cocos2d-x在现今的手游开发领域占有重要地位.那么问题来了,作为Cocos2dx的学习者,它的可移植特性我们就需要掌握,要不然总觉得少一门技能.然而这个时候各种 ...
- onhashchange事件,只需要修改hash值即可响应onhashchange事件中的函数(适用于上一题下一题和跳转页面等功能)
使用实例: 使用onhashchange事件做一个简单的上一页下一页功能,并且当刷新页面时停留在当前页 html: <!DOCTYPE html><html><body& ...
- 堆表和%%lockres%%函数
在今天的文章里,我想向你展示下SQL Server里一个未公开的函数,还有你如何用那个函数来找出在哪页记录被存储. %%lockres%% 今天我想向你展示的未公开函数叫做%%lockres%%,它与 ...
- Django--上传文件
需求 Django中forms表单上传文件处理 速查 views.py 1 2 3 4 5 6 7 8 9 def upload(request): if request.method==' ...
- CentOS6.5菜鸟之旅:纯转载Linux目录结构
来自:http://www.iteye.com/topic/1125162 使用linux也有一年多时间了 最近也是一直在维护网站系统主机 下面是linux目录结构说明 本人使用的是centos系 ...
- IOS开发UI基础storyboard相关概念的认识
本文主要介绍一些基本的概念 为后面的学习做个准备 需要了解的知识点有以下几个方面: storyboard文件的认识 IBAction 和IBOutlet UIViewController控制器的认识 ...
- mysql depended_query 优化案例一则
月度利息统计sql优化 原因:写的sql语句复杂,理解起来有难度,另一方面,查询性能比较低 原来的语句如下: SELECT tp.year, tp.month, tp.bid_id, b.`title ...
- sprint3(第三天)
今天在做的任务是整合前台和后台,使前台可以从后台得到数据 燃尽图:
- ASP.NET运行时详解 生命周期入口分析
说起ASP.NET的生命周期,网上有很多的介绍.之前也看了些这方面的博客,但我感觉很多程序猿像我一样,看的时候似乎明白,一段时间过后又忘了.所以,最近Heavi花了一段时间研究ASP.NET的源代码, ...
- 2014 WAP校园招聘笔试题
2014 WAP校园招聘笔试题 Problem's Link: http://www.doc88.com/p-6751117015483.html WAP公司笔试题 We are planning ...