Skip to content

Instantly share code, notes, and snippets.

@vikingmute
Last active December 17, 2015 10:49
Show Gist options
  • Save vikingmute/5597395 to your computer and use it in GitHub Desktop.
Save vikingmute/5597395 to your computer and use it in GitHub Desktop.
my simple vquery test ---like jquery
//vquery verson 1.0
(function(){
//the magic dollar
var $ = function(sel){
return new $.prototype.init(sel);
}
//代理方法
$.prototype.init.prototype = $.fn = $.prototype;
//原型扩展
$.prototype = {
init:function(sel){
this.selectors = document.querySelector(sel);
//如果当前为空 就新建一个
if(!this.selectors){
//新建什么类型 判断比较复杂。。。不写了
}
this.copyright = {
"version":"1.0",
"author":"viking"
}
},
css:function(prop,value){
this.selectors.style[prop] = value;
return this;
},
html:function(html){
this.selectors.innerHTML = html;
return this;
},
delete:function(){
this.selectors.parentNode.removeChild(this.selectors);
return this;
},
append:function(newnode){
this.selectors.appendChild(newnode);
return this;
},
//simple events stuff
addEvent:function(type,callback){
this.selectors.addEventListener(type,callback,false);
return this;
}
}
//add extra stuff on $ object like ajax utils...
$.ajax = function(opts,callback){
console.log('i am a xhr stuff')
}
$.ready = function(callback){
window.addEventListener('DOMContentLoaded',callback,false);
}
//exports to window
window.V = window.miniVan = $;
})()
//examples
V.ready(function(){
V('#test').css('font-size','20px').html('new hello world');
V('#test2').addEvent('click',function(e){
console.log('you clicked on div#'+e.target.id);
})
$.ajax({method:'GET',url:'fuckme.com/rest/video/1',dataType:'json'},function(data){
console.log(data);
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment