{"id":20022306,"url":"https://github.com/lucifier129/agent","last_synced_at":"2026-06-02T20:31:25.104Z","repository":{"id":23534936,"uuid":"26901703","full_name":"Lucifier129/Agent","owner":"Lucifier129","description":"代理模式：将函数调用转化为配置模式","archived":false,"fork":false,"pushed_at":"2015-01-22T09:58:17.000Z","size":352,"stargazers_count":5,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-02T03:43:45.487Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Lucifier129.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-11-20T07:51:41.000Z","updated_at":"2021-08-10T11:45:46.000Z","dependencies_parsed_at":"2022-08-22T01:31:03.675Z","dependency_job_id":null,"html_url":"https://github.com/Lucifier129/Agent","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/Lucifier129/Agent","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Lucifier129%2FAgent","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Lucifier129%2FAgent/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Lucifier129%2FAgent/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Lucifier129%2FAgent/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Lucifier129","download_url":"https://codeload.github.com/Lucifier129/Agent/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Lucifier129%2FAgent/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":33835765,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-26T15:22:16.424Z","status":"online","status_checked_at":"2026-06-02T02:00:07.132Z","response_time":109,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-13T08:39:52.289Z","updated_at":"2026-06-02T20:31:25.087Z","avatar_url":"https://github.com/Lucifier129.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"Agent\n=====\n\n代理模式：将函数调用转化为配置模式\n\n#关于Agent\n\n`Agent` 是专门针对构造函数或单例的代理模式包装策略，支持 ie6 +。\n\n它根据传入的构造函数与初始化参数，得到其实例`instance`，并返回一个代理函数。\n\n代理函数接受一个对象`options`作为参数，针对`options`对象的每一个属性`prop`， 代理函数的处理规则如下：\n\n1、如果`instance[prop]`不是一个函数，则有：\n\n```javascript\ninstance[prop] = options[prop]\n```\n\n2、如果`instance[prop]`是一个函数，则分三种情形：\n\n2.1、`options[prop]`为非数组：\n\n```javascript\ninstance[prop](options[prop])\n```\n2.2、`options[prop]`为数组：\n\n2.2.1、数组的长度大于等于2，且数据类型相同，则视为多次调用\n\n```javascript\nfor (var i = 0, len = options[prop].length; i \u003c len; i += 1) {\n    instance[prop][isArray(options[prop][i]) ? 'apply' : 'call'](instance, options[prop][i])\n}\n````\n\n2.2.2、数组长度小于2，或者大于2却不是相同数据类型，则为普通调用\n\n```javascript\ninstance[prop].apply(instance, options[prop])\n```\n\n注意：也可以将多个`options`打包成数组，传入代理函数，将在内部遍历调用\n\n了解`javascript`中的原型继承的人都知道，`instance[prop]`可以是原型对象中的方法\n\n\n#API介绍\n\n`Anget`支持amd与cmd以及commonJs模块规范，如果三者都无，则作为全局变量`agent`。\n\n如果`window.$$`为`undefined`，`Angent`将赋值给它，占用两个全局变量\n\n`test.html`中包含了`Agnet`的大多数用法，如下：\n\n```javascript\n(function() {\n\n\n        //test jQuery\n        //$$ 或 agent 返回代理函数\n        //第一个参数为构造函数或单例对象\n        //第二或之后的参数，为构造函数的参数\n        //如要代理 $('.item')，形式如下\n\n        var $item = $$($, '.item')\n\n        //下面这种形式也可以，上面为快捷方式\n/*        var $item = $('.item')\n        $item = $$($item)*/\n\n\n        var items_settings = {\n\n            prepend: '测试代理jQuery实例，调用text、css以及animate方法',\n            //数组形式，长度大于1， 为同一数据类型，即视为多次调用\n            append: ['\u003cdiv\u003e多次调用\u003c/div\u003e', '\u003cdiv\u003e多次调用\u003c/div\u003e', '\u003cdiv\u003e多次调用\u003c/div\u003e', '\u003cdiv\u003e多次调用\u003c/div\u003e'],\n\n            css: {\n                'color': '#f00'\n            },\n\n            animate: {\n                'line-height': '30px'\n            }\n\n        }\n\n        $item(items_settings)\n\n        $item([\n            {append: '多个options形式\u003cbr\u003e'},\n            {append: '多个options形式\u003cbr\u003e'},\n            {append: '多个options形式\u003cbr\u003e'},\n            {append: '多个options形式\u003cbr\u003e'},\n            {append: '多个options形式\u003cbr\u003e'},\n            ])\n\n\n        //test seajs\n\n        //seajs 是单例，可以直接用代理函数覆盖它\n\n        seajs = $$(seajs)\n\n        var seajs_settings = {\n            //相当于调用 seajs.config()\n            config: {\n                alias: {\n                    'test': './test-seajs'\n                }\n            },\n            //相当于调用 seajs.use\n            use: ['test', function(data) {\n\n                setTimeout(function() {\n                    $item({\n                        append: data\n                    })\n                }, 1000)\n\n            }]\n        }\n\n        seajs(seajs_settings)\n\n        //可以通过以下方式返回 agent 所代理的实例对象\n/*        console.log(seajs)\n        seajs = seajs()\n        console.log(seajs)*/\n\n        //test 自定义构造函数\n\n        function Person(name, age) {\n            this.name = name\n            this.age = age\n        }\n\n        Person.prototype = {\n            say: function(words) {\n\n                console.log(words || this.name)\n            },\n            sing: function(song) {\n                console.log('singing ' + song)\n            },\n            grow: function(age) {\n                this.age = age || this.age\n                console.log('My age now is ' + this.age)\n            }\n        }\n\n        var person = $$(Person, 'Jade', '24')\n\n        person({\n\n            say: '我不会被say，因为下面有个say覆盖了我',\n\n            sing: ['月亮代表我的心', '歌曲1', '歌曲2', '歌曲3', '歌曲4', '歌曲5'],\n\n            grow: 25,\n\n            //添加新属性\n            job: 'FE',\n\n            //添加新方法\n            jump: function(high) {\n                console.log('跳了' + high + '米')\n            },\n\n            //如果现有的或原型上的属性为函数，不会覆盖\n            //只会调用，并将新的值作为参数传入\n            say: function() {\n                'say函数已经存在，我将被作为参数传入，在控制台被输出'\n            }\n        })\n\n        //不管传参与否，代理函数每次都返回其所带里的实例\n        //随时可以通过赋值，找回实例，并按照以前的风格使用\n        console.log(person())\n\n\n        //test dom对象\n\n        var btn = $$(document.getElementById('btn'))\n\n        btn({\n\n            onclick: function() {\n                console.log('测试原生DOM对象')\n            },\n\n            style: {\n                background: '#f00',\n                width: '200px'\n            },\n\n            title: '一个普通title'\n\n        })\n\n        btn.alias.parse('style:stl; alt: name;')\n\n        btn({\n            stl: {\n                color:'#000'\n            },\n            name: 'test'\n        })\n\n\n        //test alias 别名\n        var $btn = $$(jQuery, '#btn')\n\n        $btn.alias.parse($btn().attr('js'))\n\n        $btn({\n            word: 'test alias',\n            sibling: '\u003cp\u003ebtn的兄弟元素\u003c/p\u003e',\n            style: {\n                color:'green',\n                background: '#eaeaea'\n            }\n        })\n\n        console.dir($btn)\n\n\n    }())\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flucifier129%2Fagent","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flucifier129%2Fagent","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flucifier129%2Fagent/lists"}