Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 347
  • Last Modified:

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 http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28504095.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

0
rwniceing
Asked:
rwniceing
  • 10
  • 7
2 Solutions
 
RobOwner (Aidellio)Commented:
V1chg() when instantiated add an object just sets the space property of your global variable but it's otherwise an empty object.
0
 
Ray PaseurCommented:
To learn JavaScript I would recommend CodeAvengers.  Short, succinct, all online, and very well written - the lessons will help you get the basic understanding you need to move forward in the language.
http://www.codeavengers.com/javascript/1#1.1

At a more advanced level you probably want to learn jQuery, but I would recommend learning the basics of JavaScript first.
http://learn.jquery.com/

The best explanation of "this" I have ever found:
http://www.digital-web.com/articles/scope_in_javascript/

Other discussion and examples:
http://www.quirksmode.org/js/this.html
http://code.tutsplus.com/tutorials/fully-understanding-the-codethiscode-keyword--net-21117
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this
0
 
rwniceingAuthor Commented:
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 ?
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
RobOwner (Aidellio)Commented:
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
0
 
RobOwner (Aidellio)Commented:
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();
0
 
RobOwner (Aidellio)Commented:
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

0
 
rwniceingAuthor Commented:
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 ?
0
 
RobOwner (Aidellio)Commented:
It's clear. But why should thisvar point to the object that's containing it? It isn't a reserved word. The "this" word is a special reserved word. The interpreter knows to reference the object if it is instantiated or defaults to the window object.
thisvar will always point to the global object unless you have a local variable with the same name in which case I've seen it use the local one.
As Ray indicated you need to do a lot of reading into learning JavaScript and object oriented programming.
0
 
RobOwner (Aidellio)Commented:
My mistake regarding the prototype. I shouldn't have mentioned it as it confused the situation so please ignore that comment.
0
 
rwniceingAuthor Commented:
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 ?"
0
 
RobOwner (Aidellio)Commented:
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

0
 
rwniceingAuthor Commented:
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.
0
 
rwniceingAuthor Commented:
Rob, you are right but it can NOT use thisvar = this  or any 'this" keyword in v1chg () function to achieve the result
0
 
RobOwner (Aidellio)Commented:
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.
0
 
rwniceingAuthor Commented:
Thanks for all of your reply
0
 
RobOwner (Aidellio)Commented:
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
0
 
rwniceingAuthor Commented:
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
0
 
RobOwner (Aidellio)Commented:
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
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 10
  • 7
Tackle projects and never again get stuck behind a technical roadblock.
Join Now