www.webdeveloper.com
Results 1 to 3 of 3

Thread: Object Scope

Hybrid View

  1. #1
    Join Date
    Aug 2011
    Posts
    1

    Object Scope

    Hi I need some help with OOP in JS...

    I have an object with a single Method to load content from a xml file. The problem is... how do I add a property to the object to store the data loaded?? I tryed adding a simple Array inside the object, but didn't work.

    Code:
    function DataCollection()
    {
    	
    	this.load = function(filename)
    	{
    		$.get(filename, function(xml){
    	             $(xml).find("marker").each(
    		    	
    		    	function(){
    		      				var name = $(this).find('name').text();
    		      				var address = $(this).find('address').text();
    		      
    		      				var lat = $(this).find('lat').text();
    		      				var lng = $(this).find('lng').text();
    		      				
    		      				var point = new google.maps.LatLng(parseFloat(lat),parseFloat(lng));
    		      				
    		      				*** HERE...add point to a collection (may an array) as property inside DataCollection object
    		      				
    		    			});
    			});
    			
    	}
    	
    }    
    
    var dc = new DataCollection();
    dc.load();

    Thanks!

  2. #2
    Join Date
    May 2011
    Posts
    194

    maybe this

    Hi,

    collection is property of object or return value

    var dc = new DataCollection();
    dc.load(); // // properties of dc created during load() ?

    var ptCollection=cd.load(); // no property but return


    function(){var name .... // local to function, no property

  3. #3
    Join Date
    Oct 2008
    Location
    U.S.
    Posts
    726
    Code:
    function DataCollection()
    {
     var obj = this; //reference to the created instance of DataCollection
            this.points = []; //or you could do: obj.points = []; here within the constructor code
     this.load = function(filename)
     {
      $.get(filename, function(xml){
                  $(xml).find("marker").each(
     
           function(){
                var name = $(this).find('name').text();
                var address = $(this).find('address').text();
     
                var lat = $(this).find('lat').text();
                var lng = $(this).find('lng').text();
     
                var point = new google.maps.LatLng(parseFloat(lat),parseFloat(lng));
     
                *** HERE...add point to a collection (may an array) as property inside DataCollection object
                obj.points.push(point);
             });
       });
     
     }
     
    }    
     
    var dc = new DataCollection();
    dc.load();
    Or, more appropriately, use prototype methods rather than define object methods in the constructor:
    Code:
    function DataCollection() {
            this.points = [];
    }    
     
    DataCollection.prototype.load = function(filename){
     var obj = this;
     $.get(filename, function(xml){
                 $(xml).find("marker").each(
     
          function(){
               var name = $(this).find('name').text();
               var address = $(this).find('address').text();
     
               var lat = $(this).find('lat').text();
               var lng = $(this).find('lng').text();
     
               var point = new google.maps.LatLng(parseFloat(lat),parseFloat(lng));
     
               *** HERE...add point to a collection (may an array) as property inside DataCollection object
               obj.points.push(point);
            });
      });
     
    };
     
    var dc = new DataCollection();
    dc.load();

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles