国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

詳解angularjs的數組傳參方式的簡單實現

2019-11-19 15:57:55
字體:
來源:轉載
供稿:網友

初學 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());});

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 重庆市| 枞阳县| 萍乡市| 潞城市| 日喀则市| 吉木乃县| 津南区| 沭阳县| 聊城市| 任丘市| 榆树市| 阜宁县| 外汇| 贵南县| 日喀则市| 英吉沙县| 博野县| 潞西市| 宝应县| 红安县| 阿拉尔市| 无棣县| 兴仁县| 河西区| 原阳县| 陕西省| 曲水县| 从江县| 安塞县| 贵南县| 瑞丽市| 益阳市| 博野县| 张家川| 浦江县| 新宾| 清涧县| 正阳县| 纳雍县| 吉首市| 蕉岭县|