JavaScript设计模式学习——Basic Function
来源:互联网 发布:删除数据库重复数据 编辑:程序博客网 时间:2024/06/06 20:24
Interface:
var Interface = function(name, methods) {
if(arguments.length != 2) {
throw new Error("Interface constructor called with " + arguments.length
+ "arguments, but expected 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]);
}
};
// Static class method.
Interface.ensureImplements = function(object) {
if(arguments.length < 2) {
throw new Error("Function Interface.ensureImplements called with " +
arguments.length + "arguments, but expected at least 2.");
}
for(var i = 1, len = arguments.length; i < len; i++) {
var interface = arguments[i];
if(interface.constructor !== Interface) {
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.");
}
}
}
};
=========================================
extend:
function extend(subClass, superClass) {
var F = function() {};
F.prototype = superClass.prototype;
subClass.prototype = new F();
subClass.prototype.constructor = subClass;
subClass.superclass = superClass.prototype;
if(superClass.prototype.constructor == Object.prototype.constructor) {
superClass.prototype.constructor = superClass;
}
}
===========================================
$:
(function(){
function _$(els){
this.elements=[];
for(var i=0,len=els.length;i<len;i++){
var element=els[i];
if(typeof element === "string"){
element=document.getElementById(element);
}
this.elements.push(element);
}
}
_$.prototype={
each:function(fn){
for(var i=0,len=this.elements.length;i<len;i++){
fn.call(this,this.elements[i]);
}
return this;
},
setStyle:function(prop,val){
this.each(function(el){
el.style[prop]=val;
});
return this;
},
show:function(){
var that=this;
this.each(function(el){
that.setStyle('display','block');
})
return this;
},
addEvent:function(type,fn){
var add=function(el){
if(window.addEventListener){
el.addEventListener(type,fn,false);
}
else if(window.attachEvent){
el.attachEvent("on"+type,fn);
}
};
this.each(function(el){
add(el);
});
return this;
}
};
window.installHelper=function(scope,name){
scope[name]=function(){
return new _$(arguments);
}
}
})();
- JavaScript设计模式学习——Basic Function
- 【学习】JavaScript设计模式——认识设计模式
- 【学习】Javascript设计模式——设计模式类别
- JavaScript设计模式学习——Singleton
- JavaScript设计模式学习——Factory
- JavaScript设计模式学习——Composite
- JavaScript设计模式学习——Decorator
- JavaScript设计模式学习——FlyWeight
- JavaScript设计模式学习——Observer
- JavaScript设计模式学习——Command
- javascript学习(11)——[设计模式]工厂模式
- 【学习】Javascript设计模式——反模式
- 【学习】Javascript设计模式——Constructor构造器模式
- 【学习】Javascript设计模式——Module模式
- JavaScript学习随记——Function
- JavaScript学习总结——Function类
- javascript 设计模式学习
- JavaScript学习记录——《学用 JavaScript 设计模式》学习笔记(3)观察者模式
- postgres分页查询实例(PHP)
- 第一博
- WebService大讲堂之Axis2(3):使用services.xml文件发布WebService
- test3
- 如何理解Crun-timelibrary(C运行时库)
- JavaScript设计模式学习——Basic Function
- WebService大讲堂之Axis2(4):二进制文件传输
- 兴趣
- Linux 的 source 命令
- hibernate常见包异常
- 怎么样上台演讲(结合ppt,实用)
- sql ,,replace替换数据类型为text的字段。。
- WebService大讲堂之Axis2(5):会话(Session)管理
- listview详解