詳解angularjs的數組傳參方式的簡單實現,詳解angularjs數組
初學 angularjs時,對 數組傳參方式感到很好奇([‘a', ‘b', function(a,b){}]),它到底怎麼實現的呢?後來由於工作很忙,對這個問題也就慢慢忘記了。
今天閑來無事,有想到了這個問題。最簡單的方法就是查看他的原始碼。無奈本人E文不好,不說看他的設計邏輯,僅看英文注釋就夠我頭疼了。嘗試閉門造車,最終竟然把車造出來了。
既然自己造的車,就要帶上自己的名(取姓名拼音第一個字母),就叫他mqyJs把,下面是示範的調用方法:
var app2 = mqyJs.applicationCreate([{ name: '直接傳入SCOPE' }, '$hello', '$world', function($scope, $hello, $world) { return $scope.name + ": " + $hello.name + $world.name;}]);
核心部分如下:
//架構開設var mqyJs = { //服務註冊 servicesList: {}, servicesRegister: function(name, value) { this.servicesList[name] = value; }, //應用建立 applicationList: [], applicationCreate: function(_opts, _args) { if (!_args) { _args = _opts; _opts = {} } _opts.scope = _opts.scope || { name: 'SCOPE沒有設定' }; if (!(_args instanceof Array)) { _args = ['$scope', _args]; } if (typeof _args[_args.length - 1] != 'function') { throw new Error('參數中沒有指定運行函數'); } _args.map((arg, index) => { if (typeof arg == 'string') { if (arg === '$scope') { _args[index] = _opts.scope; } else { if (!!arg && !(arg in this.servicesList)) { throw new Error('外掛程式:' + arg + ' 還沒有註冊'); } _args[index] = this.servicesList[arg]; } } }); return this.applicationList[this.applicationList.push({ run: function(callback) { if (typeof callback != 'function') { callback = function(_opts) { return _opts; } } return callback(_args[_args.length - 1].apply(null, _args)); } }) - 1]; }};//架構結束
通過 servicesRegister,可以註冊 服務,比如 angularjs 的 $http;
//外掛程式開始mqyJs.servicesRegister('$hello', { name: '你好'});mqyJs.servicesRegister('$world', { name: '世界'});mqyJs.servicesRegister('$china', { name: '中國'});//外掛程式結束
最終,對所有註冊的應用,自動執行
/** * 初始化完成後系統自動運行 * 比如網頁中 放到 window.onload */mqyJs.applicationList.map(function(app, index) { console.log('自動調用 -> APP #' + index + ' -> ' + app.run());});
嘗試跑一下代碼,能自動識別參數類型,完美執行。
不傳入 $scope 時,程式會自動建立一個 $scope。
//示範代碼 開始var app = mqyJs.applicationCreate(['$scope', '$hello', '$china', function($scope, $hello, $china) { return $scope.name + ": " + $hello.name + $china.name;}]);var app2 = mqyJs.applicationCreate([{ name: '直接傳入SCOPE' }, '$hello', '$world', function($scope, $hello, $world) { return $scope.name + ": " + $hello.name + $world.name;}]); var app3 = mqyJs.applicationCreate([{ name: 'world也直接傳入' }, '$hello', { name: '地球' }, function($scope, $hello, $world) { return $scope.name + ": " + $hello.name + $world.name;}]);var app4 = mqyJs.applicationCreate(function($scope) { return $scope.name;});var opts = { scope: { name: '自訂SCOPE' }};var app5 = mqyJs.applicationCreate(opts, function($scope) { return $scope.name;});app4.run(function(result) { console.log('手動調用 -> RESULT -> ' + result);});//示範代碼 結束
為了方便測試,再把代碼重新寫一遍,直接複製下面的代碼到 瀏覽器控制台即可測試
//架構開設var mqyJs = { //服務註冊 servicesList: {}, servicesRegister: function(name, value) { this.servicesList[name] = value; }, //應用建立 applicationList: [], applicationCreate: function(_opts, _args) { if (!_args) { _args = _opts; _opts = {} } _opts.scope = _opts.scope || { name: 'SCOPE沒有設定' }; if (!(_args instanceof Array)) { _args = ['$scope', _args]; } if (typeof _args[_args.length - 1] != 'function') { throw new Error('參數中沒有指定運行函數'); } _args.map((arg, index) => { if (typeof arg == 'string') { if (arg === '$scope') { _args[index] = _opts.scope; } else { if (!!arg && !(arg in this.servicesList)) { throw new Error('外掛程式:' + arg + ' 還沒有註冊'); } _args[index] = this.servicesList[arg]; } } }); return this.applicationList[this.applicationList.push({ run: function(callback) { if (typeof callback != 'function') { callback = function(_opts) { return _opts; } } return callback(_args[_args.length - 1].apply(null, _args)); } }) - 1]; }};//架構結束//外掛程式開始mqyJs.servicesRegister('$hello', { name: '你好'});mqyJs.servicesRegister('$world', { name: '世界'});mqyJs.servicesRegister('$china', { name: '中國'}); var app = mqyJs.applicationCreate(['$scope', '$hello', '$china', function($scope, $hello, $china) { return $scope.name + ": " + $hello.name + $china.name;}]);var app2 = mqyJs.applicationCreate([{ name: '直接傳入SCOPE' }, '$hello', '$world', function($scope, $hello, $world) { return $scope.name + ": " + $hello.name + $world.name;}]);var app3 = mqyJs.applicationCreate([{ name: 'world也直接傳入' }, '$hello', { name: '地球' }, function($scope, $hello, $world) { return $scope.name + ": " + $hello.name + $world.name;}]);var app4 = mqyJs.applicationCreate(function($scope) { return $scope.name;});var opts = { scope: { name: '自訂SCOPE' }};var app5 = mqyJs.applicationCreate(opts, function($scope) { return $scope.name;});app4.run(function(result) { console.log('手動調用 -> RESULT -> ' + result);}); //外掛程式結束mqyJs.applicationList.map(function(app, index) { console.log('自動調用 -> APP #' + index + ' -> ' + app.run());});
以上就是本文的全部內容,希望對大家的學習有所協助,也希望大家多多支援幫客之家。