Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4

Thread: ByRef?

  1. #1
    TNO
    TNO is offline
    Regular Coder
    Join Date
    Apr 2005
    Posts
    213
    Thanks
    2
    Thanked 1 Time in 1 Post

    ByRef?

    I don't know a better way to say than that, how would I go about passing a reference to an object instead of the object itself (like the ByRef statement in VB)?

    I have the following object as an example:

    Code:
    var myObject= function(){
    	var Stage = [
    		{id:"E1",type:"Actor",name:"bob"},
    		{id:"E2",type:"Actor",name:"fred"},
    		{id:"E3",type:"Spout",name:"krusty"},
    	];
    
    	return{
    		$ : function(id){
    			//???
    		},
    		addObject : function(obj){
    					
    		},
    		init : function(id){
    
    		},
    		render : function(){
    					
    		}
    	}
    }();

    I would like to do the following:

    myObject.$("E1").name = "Michael";

    Any ideas on how to refer to alter the object in the array directly vice a copy?

  • #2
    Regular Coder
    Join Date
    Jun 2007
    Location
    USA
    Posts
    527
    Thanks
    26
    Thanked 74 Times in 72 Posts
    Code:
    function Character(id, type, name) {
      this.id = id;
      this.type = type;
      this.name = name;
    }
    
    var obj = (function() {
      var stage = [
        new Character("E1", "Actor", "bob"),
        new Character("E2", "Actor", "fred"),
        new Character("E3", "Spout", "krusty")
      ];
      return {
        $: function(id) {
          for(var i = stage.length - 1; i >= 0; --i)
            if(stage[i].id == id)
              return stage[i];
          return null;
        },
        addObject: function(obj) {},
        init: function(id) {},
        render: function() {}
      };
    })();
    You might want to consider using an identifier other than $.
    Trinithis

  • #3
    TNO
    TNO is offline
    Regular Coder
    Join Date
    Apr 2005
    Posts
    213
    Thanks
    2
    Thanked 1 Time in 1 Post
    Sorry, that format destroys the namespace design. Only 1 global variable allowed in my situation. I'm just going to stick to this design. Its not as direct as I want, but it'll do:

    Code:
    var myObject= function(){
    	var Stage = [
    		{id:"E1",type:"Actor",name:"bob"},
    		{id:"E2",type:"Actor",name:"fred"},
    		{id:"E3",type:"Spout",name:"krusty"},
    	];
    
    	return{
    		getAttribute : function(id,attrib){
    			for(var i in Stage){
    				if(Stage[i]["id"] === id){
    					return Stage[i][attrib];
    				}
    			};
    		},
    		setAttribute : function(id,attrib,value){
    			for(var i in Stage){
    				if(Stage[i]["id"] === id){
    					Stage[i][attrib] = value;
    				}
    			};
    		}
    	}
    }();

  • #4
    Master Coder
    Join Date
    Feb 2003
    Location
    Umeå, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    Quote Originally Posted by TNO View Post
    Sorry, that format destroys the namespace design. Only 1 global variable allowed in my situation.
    You can still do it the way Trinithis suggests. Just modify the code this little bit:
    Quote Originally Posted by Trinithis View Post
    Code:
    function Character(id, type, name) {
      this.id = id;
      this.type = type;
      this.name = name;
    }
    
    var obj = (function() {
      function Character(id, type, name) {
        this.id = id;
        this.type = type;
        this.name = name;
      }
    
      var stage = [
        new Character("E1", "Actor", "bob"),
        new Character("E2", "Actor", "fred"),
        new Character("E3", "Spout", "krusty")
      ];
    
      return {
        $: function(id) {
          for(var i = stage.length - 1; i >= 0; --i)
            if(stage[i].id == id)
              return stage[i];
          return null;
        },
        addObject: function(obj) {},
        init: function(id) {},
        render: function() {}
      };
    })();
    Then if you want to use getters and setters but disallow direct unchecked setting of the value, you can add them on the Character.prototype object.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards


  •  

    Posting Permissions

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