logicslab Posted November 30, 2010 Share Posted November 30, 2010 Hi pals, I am in a straight forward doubt. I use jQuery for a long time , but not use jQuery extends method, do any one can explain it's purpose and need . I don't get not much from reading jQuery Manual. So if you have a Good idea reply me with a Good explanation and example....I can understand user can create their own custom functions, pls explain ... Thankfully Anes P.A Quote Link to comment https://forums.phpfreaks.com/topic/220236-what-is-the-actual-usage-of-jqueryextends/ Share on other sites More sharing options...
trq Posted November 30, 2010 Share Posted November 30, 2010 jQuery.extends (as the name implies) allows you to extend one object by merging another with it. The manual is pretty clear about the subject. Quote Link to comment https://forums.phpfreaks.com/topic/220236-what-is-the-actual-usage-of-jqueryextends/#findComment-1141345 Share on other sites More sharing options...
trq Posted November 30, 2010 Share Posted November 30, 2010 I should create an example. Allot of jQuery plugins use it to define default options, which can latter be merged with user inputted options. eg; (function($) { $.fn.say = function(options) { var ops = { 'word' : 'Hello' }; return this.each(function() { if (options) { // here I merge the users inputted 'options' object // with my 'ops' object. $.extend(ops, options); } $(this).html(ops.word); }); }; })(jQuery); This is just a simple jQuery plugin that when called like..... $(document).ready(function() { $('div').say(); }); Would make all div's within a document contain the html 'Hello'. Now, using $.extend() we can override the word hello with 'goodbye'. $(document).ready(function() { $('div').say({word: 'Goodbye'}); }); Quote Link to comment https://forums.phpfreaks.com/topic/220236-what-is-the-actual-usage-of-jqueryextends/#findComment-1141346 Share on other sites More sharing options...
logicslab Posted December 1, 2010 Author Share Posted December 1, 2010 Hi Thorpe, I got some idea about jQuery.extends , thanks for your help , pls verify my idea as I know depicted below: $.extend(target,objects...) --->Extends the target object with one or more specified objects.Returns the original,unmodified object. It's used mainly in creating plugins. For example, in a plugin function there need some default objects, some time we need to pass our own preference(objects), in that time we need to extend the deafault object with our own eg: (function($) { $.fn.say = function(options) { var ops = { 'word' : 'Hello' }; return this.each(function() { if (options) { // here I merge the users inputted 'options' object // with my 'ops' object. $.extend(ops, options); } $(this).html(ops.word); }); }; })(jQuery); and call this as : $(document).ready(function() { $('div').say(); }); Would make all div's within a document contain the html 'Hello'. Now, using $.extend() we can override the word hello with 'goodbye'. $(document).ready(function() { $('div').say({word: 'Goodbye'}); }); Am I right Thorpe, If my understanding has any problem advise me Regards Anes P.A Quote Link to comment https://forums.phpfreaks.com/topic/220236-what-is-the-actual-usage-of-jqueryextends/#findComment-1141701 Share on other sites More sharing options...
trq Posted December 1, 2010 Share Posted December 1, 2010 Sounds like your on the right track. Quote Link to comment https://forums.phpfreaks.com/topic/220236-what-is-the-actual-usage-of-jqueryextends/#findComment-1141721 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.