二、 构造模式
利用prototype 来构造自己的函数。
function Car(model,year,miles){
this.model = model;
this.year = year;
this.miles = miles;
}
Car.prototype.toString = function(){
return this.model + " has done " + this.miles + " miles";
};
var civic = new Car("Honda Civic",2009,20000);
var mondeo = new Car("Ford Mondeo",2010,5000);
console.log(civic.toString());
结果:
Honda Civic has done 20000 miles
一个简单的toString()方法将在所有的Car 对象之间共用。
善用构造函数,用来简单的区分它们与其他的函数
三、 单体模式
在传统的软件工程中,通过创建有一个方法的类来创建类的实例,实现单体模式。使用JavaScript,单体作为一个命名空间提供与全局命名空间隔离的实现代码,从而提供一个功能单一的访问点。
在JS中最简单的一种形式就是——一个对象直接量与它相关的方法和属性。如下:
var mySingeton = {
prototype1: "something",
prototype2: "something else",
method1: function(){
console.log('hello world');
}
}
如果你想进一步的扩展,你可以通过封闭变量和函数增加自己的私有成员和方法。只暴露那些你想公有的部分。例如:
var mySingleton = function(){
/**
* here are my private variables and method
* */
var privateVariable = "something private";
function showPrivate(){
console.log(privateVariable);
}
/**
* public variables and methods (which can access private variables and methods)
* */
return {
publicMethod: function(){
showPrivate();
},
publicVar:"the public can see this!"
}
}
var single = mySingleton();
single.publicMethod();
console.log(single.publicVar);
结果:
something private
the public can see this!
上面的代码不错了。但是让我们进一步来考虑,如果你只想在当它需要的时候才实例化它这种情况。为了节约资源,你可以把实例化的代码放在另外一个构造函数中,如下:
var Singleton = (function(){
var instantiated;
function init(){
/*singleton code here*/
return{
publicMethod: function(){
console.log('hello world')
},
publicProperty:'test'
}
}
return {
getInstance: function(){
if(!instantiated){
instantiated = init();
}
return instantiated;
}
}
})()
/*calling public methods is then as easy as:*/
Singleton.getInstance().publicMethod();
结果:
hello world
在实践中,单体模式有什么用处吗?当需要一个对象来协调整个系统的时候,单体是很有用的。最后一个关于单体的例子:
var SingletonTester = (function(){
function Singleton(args){
var args = args || {};
this.name = 'SingletonTester';
this.pointX = args.pointX || 6;
this.pointY = args.pointY || 10;
}
var instance;
var _static = {
name: 'SingletonTestet',
getInstance: function(args){
if(instance === undefined){
instance = new Singleton(args);
}
return instance;
}
};
return _static;
})();
var singletonTest = SingletonTester.getInstance({pointX:5});
console.log(singletonTest.pointX);
结果:
5
分享到:
相关推荐
https://addyosmani.com/resources/essentialjsdesignpatterns/book/ 2016年5月2日保存。
With Learning JavaScript Design Patterns, you’ll learn how to write beautiful, structured, and maintainable JavaScript by applying classical and modern design patterns to the language. If you want to...
True PDF Write reliable code to create powerful applications by mastering advanced JavaScript design patterns
Along the way you’ll discover how to create your own libraries and APIs for even more efficient coding., * Master the basics of object-oriented programming in JavaScript, as they apply to design ...
Design patterns are reusable solutions to commonly occurring problems in software design. They are both exciting and a fascinating topic to explore in any programming language.
Learning JavaScript Design Patterns 英文版,需要的可以看看
Mastering JavaScript Design Patterns
Javascript Design Patterns
Title: Implementing Cloud Design Patterns for AWS Author: Marcus Young Length: 234 pages Edition: 1 Language: English Publisher: Packt Publishing Publication Date: 2015-05-15 ISBN-10: 1782177345 ISBN-...
Design Patterns for Embedded Systems in C 英文版 First edition 2011 介绍如何使用设计模式为嵌入式系统创建高效且优化的C语言设计,这些设计方法已经实践证明非常有效。 针对嵌入式系统中发生的问题,本书的...
Create highly efficient design patterns for scalability, redundancy, and high availability in the AWS Cloud About This Book Create highly robust systems using cloud infrastructure Make web ...
javascript设计模式 英文版 pdf
mobi 格式 Implementing Cloud Design Patterns for AWS 01 Implementing Cloud Design Patterns for AWS 01
it contains the advanced concept and technology included the object oriented javascript
Beginning SOLID Principles and Design Patterns for ASP.NET Developers ■Chapter 1: Overview of SOLID Principles and Design Patterns ■Chapter 2: SOLID Principles ■Chapter 3: Creational Patterns: ...
Go Design Patterns for Real-World Projects 英文azw3 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系上传者或csdn删除
Go: Design Patterns for Real-World Projects What You Will Learn: Install and configure the Go development environment to quickly get started with your first program Use the basic elements of the ...