在不将参数传递给函数的情况下,该三元运算如何工作?

时间:2018-11-15 19:09:28

标签: javascript ternary-operator

我正在阅读有关如何制作对象和数组属性的真实副本的教程。此过程称为深层复制。这是将执行此操作的代码:

function deepExtend(source, destination) {
    for (var s in source) {
        if (source.hasOwnProperty(s)) {
            if (typeof source[s] === "object") {
                destination[s] = isArray ? [] : {};
                deepExtend(source[s],destination[s]); 
            } else {
               destination[s] = source[s];
            }
        }
    }
    function isArray(o) {
        return (typeof o === "[object Array]");
    }
}

var person = {
    array1: [1,2,4,5],
    name: "Karen",
    address: {
       street: "1 Main St",
       city: "Baltimore"
    },
    scores: [212, 310, 89],
    say: function () {
         console.log(this.name + ", " + this.address.street + ", " +
               this.address.city + ", " + this.scores );
    }
};
var employee = { salary: "$45,000" };
deepExtend(person, employee);

employee.say();  // => Karen, 1 Main St, Baltimore, 212, 310, 89

在三元运算部分,如何将参数传递给isArray函数?

destination[s] = isArray ? [] : {};

0 个答案:

没有答案