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 ...
随机推荐
- 结对编程之Fault、Error、Failure
1.结对说明 结对对象:刘世麟 博客地址:http://www.cnblogs.com/liushilin/ 双方贡献:1:1 2.题目要求 构造程序,分别是: •不能触发Faul ...
- python——第一天
两种循环: for x in …… while range(n) 生成整数序列,并且是从0开始一直到n-1的整数 raw_input() 读取的内容永远以字符串的形式,必须先用 int() 把字符串转 ...
- sql server 查找包含字符串的对象
sql server 查找包含字符串的对象 SELECT sm.object_id, OBJECT_NAME(sm.object_id) AS object_name, o.type, o.type_ ...
- EF封装类 增加版,增加从缓存中查找数据方法,供参考!
EF封装类 增加版,增加从缓存中查找数据方法,供参考! 这个类是抽象类,我这里增加了需要子类验证的方法ValidateEntity,方便扩展,若想直接使用该类,可以将该类更改成静态类,里面所有的方法都 ...
- Const的用法
宏和const的区别: 1.宏执行的是替换操作,这也就意味着会在内存中开辟多个临时空间 这样显然不是很好 2.宏不可以修改 const : 用const修饰的变量 为常量 不能修改,在内存中只有一份内 ...
- 关于Form.Close跟Form.Dispose
我们在Winform开发的时候,使用From.Show来显示窗口,使用Form.Close来关闭窗口.熟悉Winform开发的想必对这些非常熟悉.但是Form类型实现了IDisposable接口,那我 ...
- android:inputType参数类型说明
android:inputType参数类型说明 android:inputType="none"--输入普通字符 android:inputType="text" ...
- 二维KMP - 求字符矩阵的最小覆盖矩阵 - poj 2185
Milking Grid Problem's Link:http://poj.org/problem?id=2185 Mean: 给你一个n*m的字符矩阵,让你求这个字符矩阵的最小覆盖矩阵,输出这个最 ...
- 实现GridView翻页并且实现CheckBox选中功能的保持
在GridView与数据库进行绑定后,由得到的数据记录可能有许多条,以至一个页面无法容纳,这时需要进行多页显. 要实现分页显现,只要使用分页类 "PagedDataSource" ...
- edittext 监听内容变化
给EditText追加ChangedListener可以监听EditText内容变化的监听 如图是效果图 类似于过滤的一种实现 1 布局也就是一个EditText,当EditText内容发生变化时 ...