Jump to content

accessing properties outside the function


Ramu26

Recommended Posts

Hi All,

In the code below,
function Person(){
var personProperty="personPropertyValue";
//1 this works fine
this.getPersonProperty=function(){
return personProperty;
//2. this gives undefined
this.getPersonProperty=function(){
return this.personProperty;
//3. this give method not found on object Person
var getPersonProperty=function(){
return personProperty;
}
}
var o= new Person();
alert(o.getPersonProperty());
In 1 and 2, this points to current object execution context which is o, so both should produce same result, but in 2 I think
this.personProperty points to some other execution context.
In 3 as there is no "this" reference however "var" should put this in the "o" object instance and it should still work right ?
but they don't, what is happening here?
Link to comment
Share on other sites

In these objects, using var and using this create two different properties, one accessible from outside and one private. If you didn't create the variable using "this." then you can't access it using "this"

function Person() {    var self = this;    var name = "A";    this.name = "B";    this.getPrivateName = function() {        return name;    }    this.getPublicName = function() {        return self.name;    }}

Anything that is not created using "this" is not accessible outside, it's a private method or property.

Link to comment
Share on other sites

Hi Foxy Mod,

 

Thanks, Yes, if we mark something as "var" it is private and can't be accessed outside the function,so based on this behavior in 3 is correct.

 

Now, based on your explanation we are assigning "getPersonProperty" on the current object in 1 so this also works.

 

Now let us consider #2 here it is same as #1 but I have referenced the variable also with "this", it gives me "undefined", is it adding/overwriting new "this.personProperty" at runtime on the object being invoked. How does it work for #1, but #2.

 

Thanks

Link to comment
Share on other sites

In your example, you never had a line saying this.personProperty="personPropertyValue"; so it was undefined.

 

The var keyword creates a completely differen variable than the this keyword does.

// Inside one object:var myVar = 5;alert(this.myVar); // Shows undefined// Inside another object:this.myVar = 5;alert(this.myVar); // Shows "5"
Link to comment
Share on other sites

even though var creates a private variable it should be part of the object and when we say o.getPersonProperty(). still the variable declared using var should be accessible through "this" right ? because "this" refers to same function in which var is used for defining the property.

 

Why is it treating them differently ?

Link to comment
Share on other sites

Technically the private variable is not part of the object, it's not really a property at all. It's just a variable that is local to the function: the object is actually just a function on which you're allowed to append properties using this or prototype.

 

The variable created using the var keyword is in a different scope than a property that is added to the object using this.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...