Jump to content

Object, Public/private Variables, "this" Keyword


midnite

Recommended Posts

Step 1: open http://www.w3schools.com/js/tryit.asp?filename=tryjs_intro in a new browser.Step 2: copy the following codes into the left text box and press the button "Edit and Click Me >>"* because the code contains a rough testing framework written by me, and it generates a table with highlights. <table> cannot be shown here. This will be the best way to illustrate my quesion =)

<script>function myClass () {  this.public_var = "i m public";  var private_var = "i m private";  this.public_func = function () {	document.write ("in public function<br>");	document.write ("public_func->private_var (" + private_var + ")<br>");	document.write ("public_func->public_var (" + public_var + ")");  }  this.public_func_this = function () {	document.write ("in public function<br>");	document.write ("public_func->this.private_var (" + this.private_var + ")<br>");	document.write ("public_func->this.public_var (" + this.public_var + ")");  }  var that = this;}var stat_to_try = ['var myObj = new myClass ("sth");','myObj.public_func();','myObj.public_func_this();',]document.write ("<style>td,th{padding:0 5;}.err{color:red;}</style>");document.write ('<table border="1">');document.write ('<tr><th>statments to try<th>results / <span class="err">error</span>');for (var i=0; i<stat_to_try.length; i++) {  document.write ("<tr><td>"+stat_to_try[i]+"<td>");  try { eval (stat_to_try[i]);  } catch (e) { document.write ('<span class="err">'+e+"</span>");  } finally { document.write (" "); }}document.write ("</table>");</script>

alright, here's the questions:1) Why a public property is not accessible in a public method (public_func) directly? (ReferenceError: public_var is not defined)2) Why after using the "this" keyword, the public property is now can be accessed in a public method?3) Why after using the "this" keyword, the private property is now become "undefined"? (public_func->this.private_var (undefined))Isn't both public and private property can be accessed by a public method, without any problem?Thanks for your help in advance :)

Link to comment
Share on other sites

What do you want explained?

Link to comment
Share on other sites

Using that code, I can create an object of type myClass like this:var obj = new myClass();Properties and methods of that object that can be accessed through dot notation are the ones defined with the "this" keyword. So I can do these kinds of things:alert (obj.public_var);obj.public_func();Variables and functions NOT defined with the "this" keyword are used internally by the object. The following will come back as undefined:alert (obj.private_var);

Link to comment
Share on other sites

sorry for making things like a mess. and confusing you. really sorry, because i was too tired. thanks for you mates for the replies!!alright, i think i have to make it more concise, and precise. for not to confuse any more friends, i have updated my question in the original post.

Link to comment
Share on other sites

Here's another example. With this, the myClass function returns an actual object. You can see that inside the object you can refer to private variables, but you can't refer to those outside of the object (as expected, because they're private). You can refer to public variables either inside the object using the this keyword, or also outside of the object. Inside the object you don't need this to refer to the private variables, they're in the same scope as the object itself, not in the object's scope.

<html><body><script type="text/javascript">function myClass(){  var private_var = "private";  return ({	public_var: "public",		func1: function()	{	  document.write ("in public function<br>");	  document.write ("public_func->private_var (" + private_var + ")<br>");	  document.write ("public_func->public_var (" + this.public_var + ")");	},	func2: function()	{	  document.write ("in public function<br>");	  document.write ("public_func->this.private_var (" + this.private_var + ")<br>");	  document.write ("public_func->this.public_var (" + this.public_var + ")");	}  });};var stat_to_try = ['var myObj = new myClass();','myObj.func1();','myObj.func2();','document.write(myObj.public_var);','document.write(myObj.private_var);']document.write ("<style>td,th{padding:0 5;}.err{color:red;}</style>");document.write ('<table border="1">');document.write ('<tr><th>statments to try<th>results / <span class="err">error</span>');for (var i=0; i<stat_to_try.length; i++) {  document.write ("<tr><td>"+stat_to_try[i]+"<td>");  try { eval (stat_to_try[i]);  } catch (e) { document.write ('<span class="err">'+e+"</span>");  } finally { document.write (" "); }}document.write ("</table>");</script></body></html>

Link to comment
Share on other sites

1) Why a public property is not accessible in a public method (public_func) directly? (ReferenceError: public_var is not defined)
Because it is a property of the object, it can be accessed internally (by the object itself) by this.public_var, and externally by myObj.public_var . Those are the only ways.
2) Why after using the "this" keyword, the public property is now can be accessed in a public method?
That is how it was defined, in this line:this.public_var = "i m public";So all other internal references to that property must also take the form of this.public_var .
3) Why after using the "this" keyword, the private property is now become "undefined"? (public_func->this.private_var (undefined))
Here is your mistake. private_var is NOT a property. It is simply a variable. That is how it was defined, as a var. So that is how it must be accessed internally. And because it is NOT a property, it cannot be accessed externally at all. It works exactly as a var variable does in a "normal" function. It is visible only inside the function. It is not visible outside the function.Very simply: to make something a property of an object (which is to make it publicly visible) it must be defined as a property of this (an alias of the object itself). If it is defined as a variable, it will be privately visible, and publicly invisible.One reason for having private variables is to make sure they exist temporarily, either when the object is created, or when a method of the object is called. Only items defined as properties and methods (with the this keyword) are permanent (that is, memory remains allocated for them as long as the object exists). If all the values an object might use were defined as public, then they would be permanent also, and the object would require more memory, possibly a lot more memory. That becomes a big deal if you need, say, 10,000 objects in your script. The less memory they require, the faster your script will run, and the less chance you have of crashing, which of course no one wants.Also, some people create libraries of objects that might be used by other people, perhaps at a corporation, or maybe as part of a package like jquery. If you are a developer using an object that you did not design, it is helpful to know which properties and behaviors are designed to be public, and which ones are not. If there were no difference, you could easily change the value of something that should not be changed and ruin the functionality of the object. You might also design an object to be used only by yourself, but 6 months later you don't remember how you designed it. So the same potential for misusing the object comes up. Keeping the private/public distinction can help you avoid trouble.
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...