Skip to content

Instantly share code, notes, and snippets.

@congjf
Last active January 4, 2016 15:59
Show Gist options
  • Save congjf/8644025 to your computer and use it in GitHub Desktop.
Save congjf/8644025 to your computer and use it in GitHub Desktop.
JavaScript GOF
JavaScript GOF
// Normal Function
function startAnimation() {
// ...
};
function StopAnimation() {
// ...
};
// Anim Class
var Anim = function() {
// ...
};
// Add function to Anim
Anim.prototype.start = function() {};
Anim.prototype.stop = function() {};
// Another Way to add function to a class
Anim.prototype = {
start: function() {
// ...
},
stop: function() {
// ...
}
};
// Add a method to the Function object that can be used to declare methods.
Function.protype.method = function(name, fn) {
this.prototype[name] = fn;
return this;
};
var Anim = function() {};
Anim.method('start', function() {
// ...
});
Anim.method('stop', function() {
// ...
});
Anim.
method('start', function() {
// ...
}).
method('stop', function() {
// ...
});
// Function as Object
(function() {
// ...
})()
(function(a, b) {
// ...
return a + b
})(1, 2)
// Add attribute to a Object
function aFunction() {};
aFunction.aAttribute = 0;
function person() {
this.name = "Aaron";
this.age = 14;
};
person.prototype = {
getName: function() {
return this.name;
},
getAge: function() {
return this.age;
}
}
// 1.
var Book = function(isbn,title,author){
this.setIsbn(isbn);
this.setTitle(title);
this.setAuthor(author);
};
Book.prototype = {
setIsbn: function(isbn){
this.isbn = isbn;
},
getIsbn: function(){
return this.isbn;
},
setTitle: function(title){
this.title = title;
},
getTitle: function(){
return this.title;
},
setAuthor: function(author){
this.author = author;
},
getAuthor: function(){
return this.author;
}
};
// 2.
var Book = function(isbn,title,author){
this.setIsbn(isbn);
this.setTitle(title);
this.setAuthor(author);
};
Book.prototype = {
setIsbn: function(isbn){
this._isbn = isbn;
},
getIsbn: function(){
return this._isbn;
},
setTitle: function(title){
this._title = title;
},
getTitle: function(){
return this._title;
},
setAuthor: function(author){
this._author = author;
},
getAuthor: function(){
return this._author;
}
};
// 3.
var Book = function(newIsbn,newTitle,newAuthor){
var isbn,title,author;
this.setIsbn=function(newIsbn){
isbn = newIsbn;
};
this.getIsbn=function(){
return isbn;
};
this.setTitle=function(newTitle){
title = newTitle;
};
this.getTitle=function(){
return title;
};
this.setIsbn=function(newAuthor){
author = newAuthor;
};
this.getIsbn=function(){
return author;
};
}
// 4.
var Book = (function(){
// ...
return function(newIsbn,newTitle,newAuthor){
var isbn,title,author;
this.setIsbn=function(newIsbn){
isbn = newIsbn;
};
this.getIsbn=function(){
return isbn;
};
this.setTitle=function(newTitle){
title = newTitle;
};
this.getTitle=function(){
return title;
};
this.setIsbn=function(newAuthor){
author = newAuthor;
};
this.getIsbn=function(){
return author;
};
};
})();
// Constant
// 1.
var Book = function(){
var constants = ["key1": "1","key2": "2","key3": "3"];
this.getConstant = function(key){
return constants[key];
};
};
Book.getConstant("key1");
// 2.
var Book = (function(){
var constants = ["key1": "1","key2": "2","key3": "3"];
var con = function(){};
con.getContant = function(name){
return constants[name];
};
return con;
})();
Book.getConstant("key1");
// 1. 用注释描述接口
/*
interface Composite {
function add(child);
function remove(child);
function getChild(index);
}
*/
// 2. 用属性模仿接口
//
// 使用特定的方法来对特定的对象进行检查,看其是否实现了一组对应的方法,从而判断是否符合接口的要求。
function implements(object) {
for (var i = 1; i < arguments.length; i++) {
var interfaceName = arguments[i];
var interfaceFound = false;
for (var j = 0; j < object.implementsInterfaces.length; j++) {
if (object.implementsInterfaces[j] == interfaceName) {
interfaceFound = true;
break;
}
}
if (!intefaceFound) {
return false;
}
}
return true;
}
var CompositeForm = function(id, method, action) {
this.implementsInterfaces = ['Composite'];
// ...
};
function addForm(formInstance) {
if (implements(formInstance, 'composite')) {
throw new Error(Object does not implement a required interface.);
}
// ...
}
// 3. 使用鸭式辨型模仿接口
//
// 如果对象具有与接口定义的方法同名的所有方法,那么就可以认为它实现了这个接口。
// 使用这种方法需要一个辅助函数来对实现接口的方法进行判断,看某个对象是否实现了某个接口。
// 4. Interface辅助类
//
// 使用一个辅助类Interface及其类方法Interface.ensureImplements来对对象实现的方法进行显式检查。
var Interface = function(name, methods) {
if (arguments.length != 2) {
throw new Error("Interface constructor called with " + arguments.length +
"arguments, but excepted exactly 2.");
}
this.name = name;
this.methods = [];
for (var i = 0, len = methods.length; i < len; i++) {
if (typeof methods[i] !== 'string') {
throw new Error("interface constructor expects method names to be passed in as a string.");
}
this.methods.push(methods[i]);
}
};
Interface.ensureImplements = function(object) {
if (arguments.length < 2) {
throw new Error("Function Interface.ensureImplements expects arguments two and above to be instances of Interface.");
}
for (var j = 0, methodsLen = interface.methods.length; j < methodsLen; j++) {
var method = interface.methods[j];
if (!object[method] || typeof object[method] !== 'function') {
throw new Error("Function Interface.ensureImplements: object does not implement the " +
interface.name + "interface. Method" + method + "was not found.");
}
}
}
// 使用
var ResultSet = new Interface('ResultSet',['getDate','getResults']);
var ResultFormatter = function(resultsObject){
Interface.ensureImplements(resultsObject, ResultSet);
this.resultsObject = resultsObject;
};
ResultFormatter.prototype.rederResults = function(){
// ...
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment