媒介
JavaScript 的焦点是支持面向工具的,同时它也提供了强盛机动的 OOP 说话手段。本文将行使面向工具的方法,来教各人用原生js写出一个相同jQuery这样的类库。我们将会学到如下常识点:
- 闭包:镌汰变量污染,收缩变量查找范畴
- 自执行函数在工具中的运用
- extend的实现道理
- 怎样实现跨赏识器的变乱监听
- 原型链与担任
接下来我会对类库的焦点api举办讲授和展示,文章最后后附带类库的完备源码。
类库计划思绪
API先容和结果展示
变乱绑定 Xuery.on(eventName, fn) 案譬喻下:
- Xuery('#demo').on('click', function(e){
- alert('hello world!')
- })
会见和配置css Xuery.css(string|object, ?[string]) 案譬喻下:
- // 会见css
- Xuery('#demo').css('width')
- // 配置css
- Xuery('#demo').css('width', '1024px')
- // 配置css
- Xuery('#demo').css({
- width: '1024px',
- height: '1024px'
- })
会见和配置属性 Xuery.attr(string|object, ?[string]) 案譬喻下:
- // 会见attr
- Xuery('#demo').attr('title')
- // 配置attr
- Xuery('#demo').attr('title', '1024px')
- // 配置attrs
- Xuery('#demo').attr({
- title: '1024px',
- name: '1024px'
- })
会见和配置html 案譬喻下:
- // 会见
- Xuery('#demo').html()
- // 配置
- Xuery('#demo').html('前端进修原生框架')
尚有其他几个常用的API在这里就不先容了,各人可以在我的github上查察,可能基于这套基本框架,去扩展属于本身的js框架。
焦点源码
以下源码相干成果我做了注释,提议各人当真阅读,涉及到原型链和结构函数的指向的题目,是实现上述挪用方法的焦点,又不懂可以在评述区交换雷同。
- /**
- * 链模式实现本身的js类库
- */
- (function(win, doc){
- var Xuery = function(selector, context) {
- return new Xuery.fn.init(selector, context)
- };
-
- Xuery.fn = Xuery.prototype = {
- constructor: Xuery,
- init: function(selector, context) {
- // 配置元素长度
- this.length = 0;
- // 默认获取元素的上下文document
- context = context || document;
- // id选择符,则按位非将-1转化为0
- 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.context = context;
- this.selector = selector;
- return this
- },
- // 加强数组
- push: [].push,
- sort: [].sort,
- splice: [].splice
- };
-
- // 要领扩展
- Xuery.extend = Xuery.fn.extend = function(){
- // 扩展工具从第二个参数算起
- var i = 1,
- len = arguments.length,
- target = arguments[0],
- j;
- if(i === len){
- target = this;
- i--;
- }
- // 将参数工具归并到target
- for(; i<len; i++){
- for(j in arguments[i]){
- target[j] = arguments[i][j];
- }
- }
- return target
- }
-
- // 扩展变乱要领
- Xuery.fn.extend({
- 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
- }
- // ie赏识器dom2级变乱
- }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
- }
- // 不支持dom2的赏识器
- }else{
- return function(type, fn){
- var i = this.length -1;
- for(; i>=0;i--){
- this[i]['on'+type] = fn;
- }
- return this
- }
- }
- })()
- })
-
- // 将‘-’支解线转换为驼峰式
- Xuery.extend({
- camelCase: function(str){
- return str.replace(/-(w)/g, function(all, letter){
- return letter.toUpperCase();
- })
- }
- })
-
- // 配置css
- Xuery.extend({
- 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[arg[0]];
- }else{
- return getComputedStyle(this[0], false)[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].style[Xuery.camelCase(i)] = arg[0][i];
- }
- }
- }
- }else if(len === 2){
- for(var j=this.length -1; j>=0; j--){
- this[j].style[Xuery.camelCase(arg[0])] = arg[1];
- }
- }
- return this
- }
- })
-
- // 配置属性
- Xuery.extend({
- attr: function(){
- var arg = arguments,
- len = arg.length;
- if(len <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
- }
- })
-
- // 获取可能配置元素内容
- Xuery.fn.extend({
- html: function(){
- var arg = arguments,
- len = arg.length;
- if(len === 0){
- return this[0] && this[0].innerHTML
- }else{
- for(var i=this.length -1; i>=0; i--){
- this[i].innerHTML = arg[0];
- }
- }
- return this
- }
- })
-
- Xuery.fn.init.prototype = Xuery.fn;
- window.Xuery = Xuery;
- })(window, document);
(编辑:河北网)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|