Where do we use $.extend() method in jQuery?

jQueryWeb DevelopmentFront End Technology

The jQuery.extend() method is used to merge contents of two or more objects together. The object is merged into the first object. 

You can try to run the following code to learn how to use extend() method −

Example

Live Demo

<!DOCTYPE html>
<html>

<head>
<script src="https://ajax.googleapis.com/ajax/libs/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>
raja
Published on 04-Jul-2018 07:38:25
Advertisements