JS参数(对象)合并方法汇总

JavaScript022

JS参数(对象)合并方法汇总,第1张

function combineConfig(config)

{

let defconfig = {

name : "我是默认name",

age : "我是默认age:12",

}

defconfig = {...defconfig,...config}

return defconfig

}

function combineConfig_Object(config)

{

let defconfig = {

name : "我是默认name Object",

age : "我是默认age:12 Object",

}

Object.assign(defconfig, config)

return defconfig

}

function combineConfig_jquery(config)

{

let defconfig = {

name : "我是默认name jquery",

age : "我是默认age:12 jquery",

}

$.extend(defconfig, config)

return defconfig

}

function combineConfig_for(config)

{

let defconfig = {

name : "我是默认name jquery",

age : "我是默认age:12 jquery",

}

for (let k in config)

{

defconfig[k] = config[k]

}

return defconfig

}

<html>

<head></head>

<script>

function check(val)

{

alert(document.getElementById(val).value)

}

</script>

</head>

<body>

<input name="textfield" type="text" value="888" id="test">

</body>

<script>

check("test")

</script>

</html>

这样是可以的,你那样在还没有运行到<input name="textfield" type="text" value="888" id="test">时就调用check肯定获取不到对象啊

或者这样

<html>

<head></head>

<script defer>

function check(val)

{

alert(document.getElementById(val).value)

}

check("test")

</script>

</head>

<body>

<input name="textfield" type="text" value="888" id="test">

</body>

</html>

加上defer表示加载完文档以后才开始执行js