Welcome Guest. Sign in or Signup


How to force passing, as widget option, java script objects by reference rather than as values/copies?

Asked by: 1813 views


By default, when you pass a simple java script object as an option to a widget, jQuery will actually create a copy of the object within the widget creation code. This is usually fine unless you want to pass it by reference so that you can know the changes made to the object within the widget.

There is no built-in way to force passing objects by reference. However, here is a hack that will allow you to do so:

<script type="text/javascript">
	    $(document).ready(function () {
	        var Data = { "Name": "Jhon", "Age": "12", "DisplayText": true};
                 // Hack to force passing Data as reference:
                 // Define a method called "setInterval" before you pass it as an option.
	        Data.setInterval = function () { };
                 // Create the widget, passing in the above as a option. Data will be passed as reference.
	        $(div).SomeUIWidget({ DataOption: Data });
                 // After the widget is created, it's safe to remove this definition.
	        Data.setInterval = null;


  1. fishinglovers.net on Dec 18, 2014 Reply

    Appreciate you sharing, great post.Really looking forward to read more. Much obliged.

Comments on Question