DO YOU NEED A CONTENT WRITER FOR YOUR BUSINESS?

Your One-Stop Solution for All Content Needs! Click here for more!
jQuery

Difference between jQuery.extend and jQuery.fn.extend

Share

There is a significant difference between using jQuery extend() with passing one argument and doing it with two or more arguments.

Basically, jQuery.extend() merge the contents of two or more objects into the first object.

$.extend() is used to extend any object with additional functions but $.fn.extend() is used to extend the $.fn object, which in fact adds several plugin functions in one go instead of assigning each function separately.

$.extend:

var obj = { x: function() {} }
$.extend(obj, { y: function() {} });

$.fn.extend:

$.fn.extend( {
x: function() {},
y: function() {}
});
A simple example for $.extend():
<script>
var obj1 = {
apple: 5,
banana: { weight: 100, price: 200 },
cherry: 95
};
var obj2 = {
banana: { price: 400 },
durian: 200
};

$.extend( obj1, obj2 );
console.log( JSON.stringify( obj1 ) );
</script>

Result:
{“apple”:5,”banana”:{“price”:400},”cherry”:95,”durian”:200}

For more details see jQuery reference page.

A simple example for $.fn.extend():
<div>
<input type="checkbox" name="chkA"> A
<input type="checkbox" name="chkB"> B
</div>
<script>
$.fn.extend({
check: function() {
return this.each(function() {
this.checked = true;
});
},
uncheck: function() {
return this.each(function() {
this.checked = false;
});
}
});

$( "input[type='checkbox']" ).check();
</script>

Result:
All checkbox will be checked.

For more details see jQuery reference page.

When jQuery .extend() receives a single object then it adds the methods defined to it to either the jQuery or the jQuery.fn objects.

When jQuery .extend() receives two or more objects then it takes the first object and adds to it the methods and variables defined in the other objects.

Read Also:

Different way of calling a method when extending the jQuery.fn or jQuery objects:

<script>
$.extend({
methodOne: function(){...}
});
</script>

jQuery.methodOne();
<script>
$.fn.extend({
methodTwo: function(){...}
});
</script>

jQuery("div").methodTwo();
Namaste UI

For any types of queries, you can contact us on info[at]namasteui.com.

Recent Posts

Your Complete Guide to the 461 Visa: Building a Life in Australia with Your New Zealand Partner

People usually stumble onto the 461 visa when they realise there’s no simple way to…

17 hours ago

Why Roarbank Is Transforming Everyday Banking in India

In today’s digital era, people look for convenience, transparency, and genuine rewards from their financial…

21 hours ago

Quality with Quantity: Importing LED Lights Can Save You Big Bucks

If you are planning to start a lighting business and looking for a supplier of…

2 days ago

11 Reasons Why Avoiding Plagiarism is Necessary for SEO and content marketing

If you are a copywriter or a content marketer, you will second my statement that…

5 days ago

6 Common Injuries After a Motorcycle Accident

Motorcycle accidents often lead to severe injuries because riders have minimal protection compared to drivers…

1 week ago

How Automation And SEO Can Improve Customer Experience

No business owner starts his or her business with the hope of making losses. Everyone…

1 week ago