Reputation: 3321
My question is dead simple.
I just casually discovered that once you have defined a property with this.
into an object, you don't need to prepend this.
anymore when you want to call them.
So this.
is really meant to be used ad definition time, like var
?
I found it my self shortly after, i was referencing the window object with this. since i called my object without using new, so like it was a function.
One extra question, maybe for comments. Inside the main object, if i create a new object, and use this
during the object definition, this this
what will be referring to?
Upvotes: 2
Views: 1479
Reputation: 91349
No, unless the context of this
is a global object, such as window
. Take the following example:
function Foo(bar) {
this.data = bar;
console.log(this.data); // OK
console.log(data); // ReferenceError
}
In this example, you'll get a ReferenceError: data is not defined
on the first console.log(data)
, unless, data
is a global variable. To access the instance's public member, you have to use this.data
.
Upvotes: 2
Reputation: 288590
See http://jsfiddle.net/BRsqH/:
function f(){
this.public='hello!';
var hidden='TOP SECRET!';
}
var instance=new f();
alert('Public data: '+instance.public+ /* gives "hello!" */
'\nHidden data: '+instance.hidden /* gives undefined */
);
Variables created with var
are hidden and cannot be viewed nor modified outside the function which created them.
But variables created with this
are public, so you can access them outside the function.
Upvotes: 1
Reputation: 3321
I think I got it.
I defined my object as function My_Object(){...}
and then called it with MyObject()
. This way the My_Object was treated as a function, not an object and therefore this == window.
So in the end I was attaching properties and methods to window instead of My_Object! That's way there were available without prepending .this
.
The right way to initialize My_Object as an object is to call it like this new My_Object
, isn't right?
Upvotes: 0
Reputation: 707876
There are all sorts of circumstances where you MUST use this
in order to reference the right data.
These two implementations do very different things:
Array.prototype.truncate(newLen) {
// sets the length property on the current Array object
this.length = newLen;
}
Array.prototype.truncate(newLen) {
// sets a global variable named length
length = newLen;
}
var arr = [1,2,3,4,5,6,7];
arr.truncate(2);
You MUST use this
in order to control what happens if you want to modify the current object. Your assumption that you can leave it off and it will still modify the current object's properties is not correct. If you leave it off, you are modifying global variables, not member properties.
Upvotes: 2
Reputation: 120268
So this. is really meant to be used ad definition time, like var?
No, the point of this
is to be the current scope of execution. You can (and will) run into weird errors if you don't use this
. For example, imagine you are an object with a property val
and then on the prototype of that object you have
App.obj = function(){
this.val = 'initial';
}
obj.prototype.myMethod = function(val) {
// how would you assign argument val to object val?
}
also note that your reasoning breaks down with methods.
obj.prototype.meth2 = function(){
myMethod(); // fails where this.myMethod() would work.
}
Upvotes: 1