基于原型继承的链模式
来源:互联网 发布:51单片机控制电机 编辑:程序博客网 时间:2024/06/05 18:04
var A = function(selector,context){
return new A.fn.init(selector,context);
}
A.fn = A.prototype = {
constructor:A,
init:function(selector,context){
this.length = 0 ;
context = context||document;
if(~selector.indexOf('#')){
this[0] = document.getElementById(selector.slice(1));
this.length = 1;
}else{
var doms = context.getElementsByTagName(selector),i = 0 ,len = doms.length;
for(; i < len; i ++){
this[i] = doms[i];
}
this.length = len;
}
this.context = context;
this.selector = selector;
return this;
},
length : 2,
size:function(){
return this.length;
},
push:[].push,
sort:[].sort,
splice:[].splice
}
A.fn.init.prototype = A.fn;
console.log(A('demo'));
A.extend = A.fn.extend = function(){
var i = 1 , len = arguments.length,target = arguments[0],j;
if(i == len){
target = this;
i--;
}
for(; i < len ; i ++){
for(j in arguments[i]){
target[j] = arguments[i][j];
}
}
return target;
}
A.fn.extend({
camelCase:function(str){
return str.replace(/\-(\w)/g,function(all,letter){
return letter.toUpperCase();
})
},
css:function(){
var arg = arguments,
len = arg.length;
if(this.length < 1){
return this;
}
if(len == 1){
if(typeof arg[0]==='string'){
if(this[0].currentStyle){
return this[0].currentStyle[name];
}else{
return getCumputedStyle(this[0],false)[name];
}
}else if(typeof arg[0]==='object'){
for(var i in arg[0]){
for(var j = this.length -1 ; j >= 0 ; j--){
this[j].style[this.camelCase(i)] = arg[0][i];
}
}
}
}else if(len === 2){
for(var j = this.length -1 ; j >= 0 ; j --){
this[j].style[this.camelCase(arg[0])] = arg[1];
}
}
return this;
},
attr:function(){
var arg = arguments,
len = arg.length;
if(this.length < 1){
return this;
}
if(len === 1){
if(typeof arg[0]==='string'){
return this[0].getAttribute(arg[0]);
}else if(typeof arg[0]==='object'){
for(var i in arg[0]){
for(var j = this.length - 1 ; j >= 0 ; j --){
this[j].setAttribute(i,arg[0][i])
}
}
}
}else if(len === 2){
for(var j = this.length -1 ; j >=0 ; j --){
this[j].setAttribute(arg[0],arg[1]);
}
return this;
}
},
html:function(){
var arg = arguments,
len = arg.length;
if(len === 0){
return this[0]&&this[0].innerHTML;
}else{
for(var j = this.length -1 ; j >= 0 ; j--){
this[j].innerHTML = arg[0]
}
}
return this;
},
on:(function(){
if(document.addEventListener){
return function(type,fn){
var i = this.length -1;
for(; i >= 0 ; i --){
this[i].addEventListener(type,fn,false);
}
return this;
}
}else if(document.attachEvent){
return function(type,fn){
var i = this.length - 1;
for(; i >= 0 ;i --){
this[i].addEvent('on' + type,fn);
}
return this;
}
}else{
return function(type,fn){
for(; i >= 0 ;i --){
this[i].addEvent('on' + type,fn);
}
return this;
}
}
})()
})
var demo = A('div');
console.log(demo);
A('div').css({height:'30px',border:'1px solid #000'}).attr('class','demo').html('add demo text')
.on('click',function(){
console.log('clicked');
})
return new A.fn.init(selector,context);
}
A.fn = A.prototype = {
constructor:A,
init:function(selector,context){
this.length = 0 ;
context = context||document;
if(~selector.indexOf('#')){
this[0] = document.getElementById(selector.slice(1));
this.length = 1;
}else{
var doms = context.getElementsByTagName(selector),i = 0 ,len = doms.length;
for(; i < len; i ++){
this[i] = doms[i];
}
this.length = len;
}
this.context = context;
this.selector = selector;
return this;
},
length : 2,
size:function(){
return this.length;
},
push:[].push,
sort:[].sort,
splice:[].splice
}
A.fn.init.prototype = A.fn;
console.log(A('demo'));
A.extend = A.fn.extend = function(){
var i = 1 , len = arguments.length,target = arguments[0],j;
if(i == len){
target = this;
i--;
}
for(; i < len ; i ++){
for(j in arguments[i]){
target[j] = arguments[i][j];
}
}
return target;
}
A.fn.extend({
camelCase:function(str){
return str.replace(/\-(\w)/g,function(all,letter){
return letter.toUpperCase();
})
},
css:function(){
var arg = arguments,
len = arg.length;
if(this.length < 1){
return this;
}
if(len == 1){
if(typeof arg[0]==='string'){
if(this[0].currentStyle){
return this[0].currentStyle[name];
}else{
return getCumputedStyle(this[0],false)[name];
}
}else if(typeof arg[0]==='object'){
for(var i in arg[0]){
for(var j = this.length -1 ; j >= 0 ; j--){
this[j].style[this.camelCase(i)] = arg[0][i];
}
}
}
}else if(len === 2){
for(var j = this.length -1 ; j >= 0 ; j --){
this[j].style[this.camelCase(arg[0])] = arg[1];
}
}
return this;
},
attr:function(){
var arg = arguments,
len = arg.length;
if(this.length < 1){
return this;
}
if(len === 1){
if(typeof arg[0]==='string'){
return this[0].getAttribute(arg[0]);
}else if(typeof arg[0]==='object'){
for(var i in arg[0]){
for(var j = this.length - 1 ; j >= 0 ; j --){
this[j].setAttribute(i,arg[0][i])
}
}
}
}else if(len === 2){
for(var j = this.length -1 ; j >=0 ; j --){
this[j].setAttribute(arg[0],arg[1]);
}
return this;
}
},
html:function(){
var arg = arguments,
len = arg.length;
if(len === 0){
return this[0]&&this[0].innerHTML;
}else{
for(var j = this.length -1 ; j >= 0 ; j--){
this[j].innerHTML = arg[0]
}
}
return this;
},
on:(function(){
if(document.addEventListener){
return function(type,fn){
var i = this.length -1;
for(; i >= 0 ; i --){
this[i].addEventListener(type,fn,false);
}
return this;
}
}else if(document.attachEvent){
return function(type,fn){
var i = this.length - 1;
for(; i >= 0 ;i --){
this[i].addEvent('on' + type,fn);
}
return this;
}
}else{
return function(type,fn){
for(; i >= 0 ;i --){
this[i].addEvent('on' + type,fn);
}
return this;
}
}
})()
})
var demo = A('div');
console.log(demo);
A('div').css({height:'30px',border:'1px solid #000'}).attr('class','demo').html('add demo text')
.on('click',function(){
console.log('clicked');
})
0 0
- 基于原型继承的链模式
- 基于原型链的继承
- 原型模式和基于原型继承的JavaScript对象系统
- 基于原型链的继承方式
- 基于原型链继承的实现
- JavaScript面向对象(3)——原型与基于构造函数的继承模式(原型链)
- javascript基于原型的继承
- javascript 高级——基于原型链的继承
- 原型链的继承
- javaScript 基于原型的对象继承
- javascript基于原型的继承机制
- 探索javascript基于原型的继承方式
- 基于原型的对象创建和继承
- Js的基于构造函数模式的原型链模式
- Javascript继承机制的设计思想(原型链模式)
- 原型链实现继承的几种模式
- 面向对象---基于组合的继承:属性的继承基于伪装的方式实现,而方法的继承基于原型链的方式继承。
- 原型-原型链-继承
- 用Sublime批量提取单词
- 卷积神经网络入门学(1)
- 中位数与二分->以至于第k大数与二分
- 对应Xcode8.X系列下使用Alcatraz加载插件
- 机器学习(四) - - 发展历程
- 基于原型继承的链模式
- linux直接执行sql文件的方式
- c++中的inline和宏
- SqlServer 找出随机的多少条数据
- SSH框架需要包含的jar包
- ThinkPHP中,display和assign用法详解
- stdin,stdout 和STDOUT_FILENO,STDIN_FILENO
- JUC - AbstractQueuedSynchronizer(AQS) 源码分析
- android 自定义View(完美)