我对jquery框架中核心部分的理解

我对jquery框架中核心部分的理解

月光魔力鸭

2018-08-28 19:45 阅读 730 喜欢 0 jquery框架 理解jquery

jQuery 整体构造


jquery的特点就是无new构造和链式调用。

$('xxx')
$('xxx').css().attr().val();

正常的情况下应该是:

function aQuery () {
    this.name='test';
    return this;
}
console.log(new aQuery());
aquery{
    name : 'test',
    __proto__ : Object
}

如果不需要new的话,就应该在函数体内实例化。

function aQuery () {
    
    return new aQuery();//但是这样就死循环了...
}

还需要一个构造函数来进行构造:

function aQuery () {
    return new aQuery.prototype.init();
}
aQuery.prototype = {
    init : function(){
        return this;
    },
    test :function () {
        console.log('test');
    }
}
//但是这样构造的实例是init,而不是aQuery,无法访问aQuery的原型方法。
//增加一个原型指向
aQuery.prototype.init.prototype = aQuery.prototype;
//这样就可以访问aQuery的原型方法test了。
aQuery().test();

那么如何进行链式调用呢?只需要在每个函数都返回this即可。

function aQuery () {
    return new aQuery.prototype.init();
}
aQuery.prototype = {
    init : function(){
        return this;
    },
    test :function () {
        console.log('test');
        return this;
    },
    add : function(){
        //xxx
        return this;
    }
}
//这样就可以链式调用了
aQuery().test().add();//但是有个缺点,函数都没有确切的返回值。

链式调用只适用于某种特殊情况。 同时还想增加静态方法要如何来做呢?且看 extend

//我们需要提供一个接口,来增加aQuery的原型方法和静态方法。
aQuery.extend = aQuery.prototype.extend = function(object){
//虽然两个函数都指向了一个,但是可以通过this来改变
    var target = this;
    for(var key in object){
        garget[key] = object[key];
    }
    return target;//一定要返回修改后的this
};
//这样我们就可以通过extend接口增加原型函数和静态函数了
aQuery.extend({
    add : function(){
        console.log('add');
    }
});
aQuery.prototype.extend({
    add2 : function(){
        console.log('add2');
        return this;
    }
});
//可以直接调用静态函数
aQuery.add();
//可以矫勇原型函数
aQuery().add2().test();

下面是一个简单的jquery构造的实现:

'use strict';
(function(window,undefined){	
    var SchoolSelect = function(params){
        return new SchoolSelect.fn.init(params);
    };
    SchoolSelect.fn = SchoolSelect.prototype = {
            constructor : SchoolSelect,
            init : function(params){
                this.cfg = params;
                return this;
            },
            version : '1.0'
    };
    SchoolSelect.fn.init.prototype = SchoolSelect.fn;
    SchoolSelect.extend = SchoolSelect.fn.extend = function(obj) {
        var target = this;
        for(var k in obj){
            target[k] = obj[k];
        }
        return target;
    };
    SchoolSelect.extend({
        add : function(){
            console.log('aaa');
        },
        age : 11
    });
    SchoolSelect.fn.extend({
        abc : function(){
            console.log('bbb');
            return 'aaccc';
        }
    });
    window.SchoolSelect = SchoolSelect;
})(window);

转载请注明出处: https://chrunlee.cn/article/jquery-personal-explain.html


感谢支持!

赞赏支持
提交评论
评论信息 (请文明评论)
暂无评论,快来快来写想法...
推荐
关于web打印,需要对页面内容进行页面样式设置,呈现出分页的样子,同时对于题目中的图片或表格尽量不分到两个页面中,因此实现了一个jquery的web打印插件,当然,这个插件目前只适用于部分情况,仅供借鉴
本文概括了递归、闭包、原型、继承,理清这些基本的概念,有助于你接纳更多的东西,我们会在下一个章节对函数进行更深入的讨论。
近期需求:将一棵树导出到excel中,树是ztree,通过插件Table2excel导出table到excel中。
对于web开发过程中的JS对象 Array ,我们真的充分使用了么?是不是理解了Array的全部?能够在合适的地点调用合适的函数,使用合适的属性?
在项目开发过程中,分页是少不了的,之前封装了一个分页组件,样式是基于bootstrap的样式,当然也可以自己来修改
在开发过程中多个页面使用的一个小工具类,简单完善了下,还算不错,给各位提供下小思路。
nvm install 16.15.0 : The process cannot access the file because it is being used by another process
前端时间搞了个小转码,放在后台,但是特别占带宽,想着能不能从前台把这个事搞定呢?读取图片的二进制,然后将字节流处理后重新生成图片展示处理啊。