我有以下代码:
var object1 = function() {};
var object2 = {prop: "hello"};
$.extend(object1, object2)
在 Chrome 开发者控制台中,当输入 object1
时,仅返回函数。但我可以输入 object1.prop
。这是为什么?
最佳答案
jquery doc 中的 extend
方法:
Description: Merge the contents of two or more objects together into the first object.
当你这样做时:
$.extend(object1, object2)
你的意思是“获取object2
的属性(prop = "hello"
)并将它们放到object1
上。因此您所做的就是为 object1
提供一个名为 prop
的属性。
另一个例子:
// this object
var colours = { red: 1, green: 2, blue: 3 };
// will be merged into this one below
var people = { bob: 4, joe: 5 };
// like this!
$.extend(people, colours);
console.log(people);
// prints { red: 1, green: 2, blue: 3, bob: 4, joe: 5 }
关于javascript - 使用 jQuery 扩展方法将函数与对象合并,为什么只返回函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33949051/