• <fieldset id="8imwq"><menu id="8imwq"></menu></fieldset>
  • <bdo id="8imwq"><input id="8imwq"></input></bdo>
    最新文章專題視頻專題問答1問答10問答100問答1000問答2000關(guān)鍵字專題1關(guān)鍵字專題50關(guān)鍵字專題500關(guān)鍵字專題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關(guān)鍵字專題關(guān)鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
    問答文章1 問答文章501 問答文章1001 問答文章1501 問答文章2001 問答文章2501 問答文章3001 問答文章3501 問答文章4001 問答文章4501 問答文章5001 問答文章5501 問答文章6001 問答文章6501 問答文章7001 問答文章7501 問答文章8001 問答文章8501 問答文章9001 問答文章9501
    當前位置: 首頁 - 科技 - 知識百科 - 正文

    EffectiveJavaScriptItem55接受配置對象作為函數(shù)參數(shù)

    來源:懂視網(wǎng) 責編:小采 時間:2020-11-27 20:31:22
    文檔

    EffectiveJavaScriptItem55接受配置對象作為函數(shù)參數(shù)

    EffectiveJavaScriptItem55接受配置對象作為函數(shù)參數(shù):雖然保持函數(shù)接受的參數(shù)的順序很重要,但是當函數(shù)能夠接受的參數(shù)達到一定數(shù)量時,也會讓用戶很頭疼:var alert = new Alert(100, 75, 300, 200, "Error", message, "blue", "white", &qu
    推薦度:
    導讀EffectiveJavaScriptItem55接受配置對象作為函數(shù)參數(shù):雖然保持函數(shù)接受的參數(shù)的順序很重要,但是當函數(shù)能夠接受的參數(shù)達到一定數(shù)量時,也會讓用戶很頭疼:var alert = new Alert(100, 75, 300, 200, "Error", message, "blue", "white", &qu

    雖然保持函數(shù)接受的參數(shù)的順序很重要,但是當函數(shù)能夠接受的參數(shù)達到一定數(shù)量時,也會讓用戶很頭疼:

    var alert = new Alert(100, 75, 300, 200,
     "Error", message,
     "blue", "white", "black",
     "error", true);

    隨著函數(shù)的不斷重構(gòu)和進化,它能夠接受的參數(shù)也許會越來越多,最終就像上面的例子那樣。

    對于這種情況,JavaScript可以使用一個配置對象來替代以上的所有參數(shù):

    var alert = new Alert({
     x: 100, y: 75,
     width: 300, height: 200,
     title: "Error", message: message,
     titleColor: "blue", bgColor: "white", textColor: "black",
     icon: "error", modal: true});

    這樣做雖然會讓代碼稍微冗長一些,但是毫無疑問它的好處是很明顯的:配置對象中的每個屬性的名字就好比是一份文檔,來告訴用戶這個屬性是干什么用的。特別是對于布爾值,單獨的傳入true和false是很難判斷它的真實意圖的。

    使用這種方式的另外一個好處是,所有的屬性都是可選的。如果配置對象中沒有出現(xiàn)某個屬性,那么就是用默認值來代替它。

    var alert = new Alert(); // use all default parameter values

    如果函數(shù)需要接受必須的參數(shù),那么最好還是將它放在配置對象的外面,就像下面這樣:

    var alert = new Alert(app, message, {
     width: 150, height: 100,
     title: "Error",
     titleColor: "blue", bgColor: "white", textColor: "black",
     icon: "error", modal: true});

    配置對象中的所有屬性都是函數(shù)能夠接受的可選參數(shù),而app和message則是必須要傳入的參數(shù)。

    對于配置對象的處理,可以像下面這樣:

    function Alert(parent, message, opts) {
     opts = opts || {}; // default to an empty options object
     this.width = opts.width === undefined ? 320 : opts.width;
     this.height = opts.height === undefined
     ? 240
     : opts.height;
     this.x = opts.x === undefined
     ? (parent.width / 2) - (this.width / 2)
     : opts.x;
     this.y = opts.y === undefined
     ? (parent.height / 2) - (this.height / 2)
     : opts.y;
     this.title = opts.title || "Alert";
     this.titleColor = opts.titleColor || "gray";
     this.bgColor = opts.bgColor || "white";
     this.textColor = opts.textColor || "black";
     this.icon = opts.icon || "info";
     this.modal = !!opts.modal;
     this.message = message;
    }

    對于可選的配置對象,首先使用Item 54中介紹的方法當它在真值判斷中返回false時,使用空對象替換它。

    上述的代碼還有進一步優(yōu)化的空間:通過使用對象擴展或者合并的函數(shù)。在很多JavaScript的庫和框架中都有一個extend函數(shù),它接受一個目標對象和一個源對象,然后將源對象中的屬性拷貝到目標對象中:

    function Alert(parent, message, opts) {
     opts = extend({
     width: 320,
     height: 240
     });
     opts = extend({
     x: (parent.width / 2) - (opts.width / 2),
     y: (parent.height / 2) - (opts.height / 2),
     title: "Alert",
     titleColor: "gray",
     bgColor: "white",
     textColor: "black",
     icon: "info",
     modal: false
     }, opts); this.width = opts.width; this.height = opts.height; this.x = opts.x; this.y = opts.y; this.title = opts.title; this.titleColor = opts.titleColor; this.bgColor = opts.bgColor; this.textColor = opts.textColor; this.icon = opts.icon; this.modal = opts.modal;
    }

    通過extend函數(shù),不再需要時常對每個屬性進行判斷。上述代碼中的第一個extend函數(shù)用來在width和height屬性沒有被設(shè)置使設(shè)置默認值,因為在第二個extend函數(shù)中會根據(jù)它們進行計算。

    如果所有的屬性最終會被賦值到this對象上,那么以上代碼可以簡化成下面這樣:

    function Alert(parent, message, opts) {
     opts = extend({
     width: 320,
     height: 240
     });
     opts = extend({
     x: (parent.width / 2) - (opts.width / 2),
     y: (parent.height / 2) - (opts.height / 2),
     title: "Alert",
     titleColor: "gray",
     bgColor: "white",
     textColor: "black",
     icon: "info",
     modal: false
     }, opts);
     extend(this, opts);
    }

    extend函數(shù)的實現(xiàn)通常都會遍歷源對象的屬性,然后如果該屬性的值不是undefined時,會將它拷貝到目標對象上:

    function extend(target, source) {
     if (source) {
     for (var key in source) {
     var val = source[key];
     if (typeof val !== "undefined") {
     target[key] = val;
     }
     }
     }
     return target;
    }

    總結(jié)

    使用可選的配置對象來讓API更具可讀性。

    配置參數(shù)中的屬性都應該是函數(shù)的可選參數(shù)。

    使用extend工具函數(shù)來簡化使用了配置對象的代碼。

    聲明:本網(wǎng)頁內(nèi)容旨在傳播知識,若有侵權(quán)等問題請及時與本網(wǎng)聯(lián)系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

    文檔

    EffectiveJavaScriptItem55接受配置對象作為函數(shù)參數(shù)

    EffectiveJavaScriptItem55接受配置對象作為函數(shù)參數(shù):雖然保持函數(shù)接受的參數(shù)的順序很重要,但是當函數(shù)能夠接受的參數(shù)達到一定數(shù)量時,也會讓用戶很頭疼:var alert = new Alert(100, 75, 300, 200, "Error", message, "blue", "white", &qu
    推薦度:
    • 熱門焦點

    最新推薦

    猜你喜歡

    熱門推薦

    專題
    Top
    主站蜘蛛池模板: 国产成人精品日本亚洲专区| 丁香色婷婷国产精品视频| 久久精品国内一区二区三区| 亚洲国产午夜中文字幕精品黄网站| 精品一区二区在线观看| 亚洲AV无码成人网站久久精品大| 精品国产一区二区22| 久久久国产精品福利免费| 精品人妻va出轨中文字幕| 亚洲国产精品狼友中文久久久| 99热这里只有精品国产66| 国产欧美日韩精品a在线观看| 亚洲欧美精品一区久久中文字幕| 国产精品亚洲玖玖玖在线观看| 99久久精品国产麻豆| 人妻少妇精品中文字幕AV| 下载天堂国产AV成人无码精品网站 | 欧美精品在线一区| 国产成人精品一区二区秒拍| 人妻少妇精品视频一区二区三区| 亚洲а∨天堂久久精品9966| 青草青草久热精品视频在线观看| 国产亚洲午夜高清国产拍精品| 最新亚洲精品国自产在线观看| 久久九九亚洲精品| 亚洲日韩精品欧美一区二区| 日韩精品在线免费观看| 欧美精品国产日韩综合在线| 久久国产亚洲精品麻豆| 欧美精品1区2区| 欧美亚洲国产精品久久蜜芽| 国产夫妇精品自在线| 国内精品免费视频精选在线观看| 国产精品久久久久久福利漫画 | 久久综合久久自在自线精品自| 亚洲国产精品无码专区影院| 中文字幕日韩精品有码视频| 亚洲国产精品嫩草影院在线观看| 无码人妻精品一区二区三区东京热| 久久精品国产网红主播| 99久久成人国产精品免费|