在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問(wèn)答/HTML/ 引入一個(gè)js文件報(bào)錯(cuò)TypeError: Cannot set property

引入一個(gè)js文件報(bào)錯(cuò)TypeError: Cannot set property 'extend' of undefined

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>

</head>
<body>

<script src="./aid.js"></script>
<script>
    function abc(){
        console.log('我是小abc')
    }

    var object1 = {
        apple: 0,
        banana: { weight: 52, price: 100 },
        cherry: 97
    };
    var object2 = {
        banana: { price: 200 },
        durian: 100
    };

    var object = Aid.extend({}, object1, object2);
</script>



</body>
</html>
var Aid = function() {
    var isFunctionCharacter = function(obj)
    {
        try {
            if(typeof obj === "function") { //是函數(shù)    其中 FunName 為函數(shù)名稱(chēng)
                console.log("is function");
            } else { //不是函數(shù)
                console.log("not is function");

            }
        } catch(e) {}
    }
    return { isFunction : isFunctionCharacter };
}();

Aid.fn = Aid.prototype

Aid.extend = Aid.fn.extend = function() {
    var options, name, src, copy, copyIsArray, clone,
        target = arguments[0] || {}, // 默認(rèn)第0個(gè)參數(shù)為目標(biāo)參數(shù)
        i = 1,    // i表示從第幾個(gè)參數(shù)凱斯想目標(biāo)參數(shù)進(jìn)行合并,默認(rèn)從第1個(gè)參數(shù)開(kāi)始向第0個(gè)參數(shù)進(jìn)行合并
        length = arguments.length,
        deep = false;  // 默認(rèn)為淺度拷貝

    // 判斷第0個(gè)參數(shù)的類(lèi)型,若第0個(gè)參數(shù)是boolean類(lèi)型,則獲取其為true還是false
    // 同時(shí)將第1個(gè)參數(shù)作為目標(biāo)參數(shù),i從當(dāng)前目標(biāo)參數(shù)的下一個(gè)
    // 處理深度拷貝情況
    if (typeof target === 'boolean') {
        deep = target;

        // Skip the boolean and the target
        target = arguments[i] || {};
        i++;
    }


    // 判斷目標(biāo)參數(shù)的類(lèi)型,若目標(biāo)參數(shù)既不是object類(lèi)型,也不是function類(lèi)型,則為目標(biāo)參數(shù)重新賦值
    if (typeof target !== 'object' && !Aid.isFunction(target)) {
        target = {};
    }
}
回答
編輯回答
葬愛(ài)

Aid 是一個(gè)object, 返回 { isFunction : isFunctionCharacter }, 他又不是一個(gè)構(gòu)造函數(shù), 你怎么在他的原型里添加方法?

Aid是模仿jquery做的一個(gè)方法吧? 那么

var Aid = function() {
    var isFunctionCharacter = function(obj)
    {
        try {
            if(typeof obj === "function") { //是函數(shù)    其中 FunName 為函數(shù)名稱(chēng)
                console.log("is function");
            } else { //不是函數(shù)
                console.log("not is function");

            }
        } catch(e) {}
    }
    return { isFunction : isFunctionCharacter };
}
// ();

這里的立即執(zhí)行應(yīng)該去掉吧, 這樣應(yīng)該可以

2017年4月11日 23:33
編輯回答
使勁操

Aid.fn.extend 報(bào)錯(cuò),Aid.fn = Aid.prototype,但是Aid不是函數(shù)呀,Aid.prototype是undefined。再Aid.fn = Aid.prototype的后面console.log(Aid)看一下

2018年5月16日 04:58
編輯回答
貓小柒

你可能需要換一種寫(xiě)法,讓函數(shù)聲明提升

(function Aid() {
    var isFunctionCharacter = function(obj)
    {
        try {
            if(typeof obj === "function") { //是函數(shù)    其中 FunName 為函數(shù)名稱(chēng)
                console.log("is function");
            } else { //不是函數(shù)
                console.log("not is function");

            }
        } catch(e) {}
    }
    return { isFunction : isFunctionCharacter };
})();
2017年1月29日 14:29