我们在jQuery的哪里使用$ .extend()方法?
jQuery.extend()方法用于将两个或更多对象的内容合并在一起。该对象将合并到第一个对象中。
您可以尝试运行以下代码以了解如何使用extend()方法-
示例
<!DOCTYPE html>
<html>
<head>
<script src="https://cdn.staticfile.org/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function() {
$("#button1").click(function() {
var obj1 = {
maths: 60,
history: {pages: 150,price: 400,lessons: 30},
science: 120
};
var obj2 = {
history: { price: 150, lessons: 24 },
economics: 250
};
$.extend(true, obj1, obj2);
$("#demo").append(JSON.stringify(obj1));
});
});
</script>
</head>
<body>
<button id="button1">Result</button>
<div id="demo"></div>
</body>
</html>