(1)创建全局jQuery对象
(function(global,document){
jQuery.prototype=InstanceCreation.prototype;
jQuery.extend=jQuery.prototype.extend=extend;
function jQuery(selector){
return new InstanceCreation(selector);
}
function InstanceCreation(selector){
var instance=this;
instance[0]=document.querySelector(selector);
return this;
}
function extend(material){
var depository=this;
for(var property in material){
if(!material.hasOwnProperty(property)){
continue;
}
depository[property]=material[property];
}
return this;
};
//export:
global.jQuery=jQuery;
}(window,document));
(2)扩展jQuery.prototype,然后调用
<input id=testButton type=button value=test />
<script>
(function($){
$.prototype.extend({
click:function(){
var $button=this,
htmlButton=$button[0],
clickEventHandler=arguments[0];
htmlButton.addEventListener('click',function(){
clickEventHandler.call($button);
},false);
return this;
}
});
$('#testButton').click(function(){
var $button=this,
htmlButton=$button[0];
alert(htmlButton.value);
});
}(jQuery));
</script>