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

JavaScript014

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

}

>var a = 3

undefined

>a

3

>var str = "12345678"

undefined

>str

'12345678'

>str.substr(a)

'45678'

>str.substr('+a+')

'12345678'

>str.substr(a-1)

'345678'

nodejs

中的执行结果,因为

a

==

3,所以你调用

str.substr(a)

就等于调用了

str.substr(3),而

str.substr(a-1)

就等同于调用

str.substr(2)。进一步的,你调用

str.substr('+a+'),实际上是往

substr

方法里传了一个字符串

+a+,字符串不是数字,substr

把它理解为了0。

使用变量作为参数和使用字面量作为参数没有任何区别,在方法执行那一刻变量的值是什么,那么就相当于你用那个值的字面量去调用方法。

<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