SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Object creation question

    I am using this syntax to create an object:
    Code:
    carObject = {
    	model : "Volvo",
    	color : "blue",
    	outputModel : function()
    	{
    		alert("Modelname is: " + this.model)
    	}
    };
    
    carObject.outputModel(); // alerts "Modelname is Volvo"
    Lets say i would like to add a new property, called brand, thats is equal to model. Can anyone please explain why this doesnt work?
    Code:
    carObject = {
    	model : "Volvo",
    	color : "blue",
    	brand : this.model, 
    	outputModel : function()
    	{
    		alert("Modelname is: " + this.brand)
    	}
    };
    
    carObject.outputModel(); // alerts "Modelname is undefined"

  2. #2
    SitePoint Evangelist vikrantkorde's Avatar
    Join Date
    Jun 2004
    Location
    Mumbai, India
    Posts
    541
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Lightbulb

    Hi Pepejeria,
    I can;t tell you why the above code is not working but this code works

    Code:
      carObject = {
          model : "Volvo",
          brand : ""+this.model+"",
          color : "blue",
          outputModel : function()
          {
              this.setval();
              alert("Modelname is: " + this.brand)
          },
          setval : function() {
              this.brand=this.model;
          }
      };
      
      carObject.outputModel(); // alerts "Modelname is Volvo"

    What i can guess is that you can't access the value of the variable in the variable area itself, rather it can be accessed only from functions.
    Vikrant Korde
    S Y S T I M E, Mhape,
    Mumbai, Maharashtra, India.

  3. #3
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The bible says:

    A costructor is a js function with two special features:

    --The function is invoked using the new operator.
    --The function is passed a reference to a newly created, empty object as the value of the this keyword, and it is responsible for performing initialization for that new object. ("Javascript: The Definitive Guide" (Flanagan))
    On the other hand, what you are using is an 'object literal', and apparently the assigning of an object literal to a variable name does not cause the 'this' keyword for that variable name to be passed to the object literal.

    However, all function are passed the 'this' keyword--even global functions. Global functions are really properties of the window object, so in a global function, this refers to the window object. For other functions, this refers to the calling object.

  4. #4
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Pepejeria
    Lets say i would like to add a new property, called brand, thats is equal to model. Can anyone please explain why this doesnt work?
    Code:
    carObject = {
    	model : "Volvo",
    	color : "blue",
    	brand : this.model, 
    	outputModel : function()
    	{
    		alert("Modelname is: " + this.brand)
    	}
    };
    
    carObject.outputModel(); // alerts "Modelname is undefined"
    "this" is a pointer to the "execution context", i.e. object the current function was called for. In javascript, "called for" means one of the following:

    -- function was called using dot notation: object.func()
    -- function is a constructor: object = new func();
    -- function is called indirectly via call() or apply()

    Since neither takes place in given case, this takes a default value, namely "window" (in browser).

    The common way to achieve this is to use constructors:

    Code:
    function carObject() {
    	this.model =  "Volvo";
    	this.brand = this.model;
    	this.outputModel = function()
    	{
    		alert("Modelname is: " + this.brand)
    	}
    }
    
    alert((new carObject()).brand)
    "Delayed initialization" method shown by vikrantkorde can be also useful:

    Code:
      carObject = {
          model : "Volvo",
          outputModel : function()
          {
    	  	if(typeof(this.brand) == "undefined")
    			this.brand = this.model;
              alert("Modelname is: " + this.brand)
          }
      }
      
      carObject.outputModel();

  5. #5
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you for the answers, you guys rock


Bookmarks

Posting Permissions

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