Link to home
Start Free TrialLog in
Avatar of rwniceing
rwniceing

asked on

how to let or set global thisvar object as "this" keyword object in javascript;

Dear Experts,

"this" keyword is easy to use but sometimes it is hard to follow when script
programming is huge . So I wrote question at https://www.experts-exchange.com/questions/28504095/this-on-javascript.html

And I would like to continue the last  question in the post for global thisvar with this object. Please read the following code for this and thisvar testing, how
to let thisvar object this Or it is no way because this keyword is set different from other global object by browser javascript interpreter ?
Now b.space for  b= new v1chg() is undefined not showing "v1";
<script type="text/javascript">
var thisvar =new Object();
function v1(){
this.space="v1";
}
function v1chg(){
thisvar.space="v1";
}
var b=new v1();
console.log(b.space) ;//"v1";
var b=new v1chg();
console.log(b.space) ;//"undefined";
</script>

Open in new window

Avatar of Rob
Rob
Flag of Australia image

V1chg() when instantiated add an object just sets the space property of your global variable but it's otherwise an empty object.
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rwniceing
rwniceing

ASKER

In other words, I mean why thisvar won't refer to V1chg object when var b=new V1chg() instantiating but this does on var b=v1() ? It is controlled by browser interpreter ?
No, not sure what you're missing.

Inside v1() if you use "this" it will either refer to the window object if you use v1 as a function, OR the object if you use it as a constructor.

thisvar will always refer to your global object regardless if the scope
This will do your head in, but you can use prototype to inherit settings from another object.
http://jsbin.com/negac/1/edit

After your two functions v1 and v1chg, add this line (demo in the link above)
v1chg.prototype = new v1();
After adding the prototype, v1chg now has the internal code of v1, i.e.

this.space="v1"; //sets an internal variable "space"
thisvar.space="v1"; // sets the "space" property of the "thisvar" global variable 

Open in new window

It is not related to prototype on this thread. It is just concept question. it seems ray's attached link at http://www.digital-web.com/articles/scope_in_javascript/ is explaining this concept.

why this keyword is referring to object v1 when var b=new v1() is instantiating
but thisvar global object is NOT referring to object v1chg when var b=new v1chg is instantiating.

The question is clear, right ?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
My mistake regarding the prototype. I shouldn't have mentioned it as it confused the situation so please ignore that comment.
Or the question may be  set to "how to make global object same as "this" keyword object without this keyword assistance involved if know actual meaning of "this" keyword ?"
I'm not sure if this is what you mean:

var thisvar = new Object();

function v1(){
	thisvar = this;
	this.space="v1";
}

function v1chg(){
	thisvar = this;
	thisvar.space="v1";
}

var b=new v1();
console.log(b.space) ;//"v1";

b=new v1chg();
console.log(b.space) ;//"v1";

Open in new window

I think since there is  a lot terminology or technical writing term from javascript document that will
let reader not to finish the whole article or tutorial from javascript. They hang around (be stopped) on those unclear or abstract terminology word such as "context" and "scope" or "object literal"  and give up
tp complete the reading. for example,
I always confusing the meaning of "context" and "scope" , the following link  is good and easy to understand of it.  After clarify the meaning of scope and context, now read back those related
"this" keyword article or your all experts' attached links  more easily.

http://ryanmorr.com/understanding-scope-and-context-in-javascript/
http://blog.kevinchisholm.com/javascript/context-object-literals/


How about to conclude my question with this answer as follow ?

The answer is that "this" keyword is reserverd word for javascript programming which is
not Global object, and the usage of this keyword is  described on those attached link in this post
Copying this keyword to the global object that may not be easy and author's topic question is wanting to know more about this keyword operation through the code example on the topic post.
Rob, you are right but it can NOT use thisvar = this  or any 'this" keyword in v1chg () function to achieve the result
sounds like you've got it.  the key is "this" is a reserved word and is interpreted differently to other words.  And that's right you can't use "this" in the way you are wanting to.
Thanks for all of your reply
rwniceing,
Can you clarify for me the reason you've spilt the points as such and why you've accepted http:#a40281320 as the solution?
The links provided are good reference material but what exactly answered your original question?
My subsequent comments I would've thought answered your original question in detail so I'm confused on the close.
Would appreciate your thoughts.
Rob
Do you have other idea or better  answer  to "how to let or set global thisvar object as "this" keyword object in javascript;"  on topic post ?

Please advise

Rwniceing
As I said before, "this" is s reserved keyword that changes based on the scope/context do you can't set a global variable to "this". I demonstrated in http:#a40281394 of assigning the current context of "this" to the global variable, but all that did was assign a link to the v1 or v1chg object, NOT the "this" pseudonym