Hi everyone
I came from Java so there's somethings that I would like to
clear up:
1) What's the purpose of var?
To declare a variable. Java is block scoped, javascript is not, and
its scoping units are functions. A variable declared inside a function
body is only visible to code inside (lexically inside) the same
function (the code of the function's body, including any nested
functions).
Since JS has no "forced types"
(i don't know if I am using the right term), why
do we bother to declare a variable like
var a;
a = 1;
Given that code, "we" would write - var a = 1; - as there is little
point in splitting it up there.
when we can simply just do this?
a = 1; (without implicitly declaring var a before hand?)
Assignments to undeclared Identifiers result in the creation of
properties of the global object. These may, in most relevant senses,
act as global variables (properties of the global object added by
executing code can be deleted, while real global variables cannot
(people don't tend to attempt to delete global variables so the
practicalities of deleting don't have much impact)).
There is a general programming axiom that things should never be given
more scope than they need, so creating what are effectively global
variables where function local variables could be used would be a bad
idea. And certainly introduces considerable additional potential for
naming conflicts. Consider, for example, a loop counter which in java
would traditionally be named "i", and a loop that included a call to a
function that also executed a loop with a counter named "i". If the
two "i"s were global then the loop counter for the first loop would be
being modified by the loop code in the function it called.
It may then be argued that global variables do not need to be
declared. There are two reasons for not doing that: the first is code
maintainability/understandably; you encounter - a = 1; - in function
body code and wonder if the intention is to assign to a global
variable or whether this is an example of failing to declare - a - as
a function local variable. If you find a global - var a; - declaration
then you know the intention, otherwise you have to look for other uses
of/references to a global - a - (and if there are none that implies
looking at all the code before that can be determined).
The second reason for declaring all global variables is a peculiarity
of IE browsers. If an Element in the DOM is given an ID attribute, say
- ID="a", and there is no declaration of a correspondingly named
global variable, then IE creates a property of the global/window
object with a name that corresponds to the ID attribute's value.
Having created this, effectively, global variable to refer to the DOM
Element it then throws exceptions if you attempt to assign to that
variable. So, in this example, - a = 1; - would result in an exception
being thrown. If you declare - var a; - globally IE does not assign
the Element reference to that variable, and does not throw exceptions
if you assign to it.
Apart from being quite difficult to track down, this IE issues makes
javascript code vulnerable to modifications in HTML. If you are using
a non-declared global variable and someone else modifies the HTML
(which is not unknown in collaborative projects) such that they
introduce an element with an ID attribute that happens to coincide
with that global variables then the script suddenly breaks, but only
in IE.
2) When do we use new? In Java, we use new when we are
constructing a new class that has a constructor. But in
Javascript, I don't think I have come across the concept of
"classes".
Not as part of the language. The concept of 'class' can be employed in
(OO) code design.
For example,
//constructor, (still a function)
function Jar(param){
this.member = param;
}
why is it such that I have to use the following:
var myJar = new Jar('abc'); //Note that this
requires "new" here
in order to declare this?
Applying the - new - operator to a function causes a new object to be
created, the (current) value of the function's - prototype - property
to be assigned to the new object's internal [[Prototype]] property
(for use as the start of its prototype chain), and then for that
function's body to be executed with the - this - value assigned a
reference to the new object (and if the function's body does not
return any other object reference then the result of the - new -
expression is a reference to that newly created object). The reason
for using the - new - operator is that you want these things to
happen, and you use it when you want these things to happen.
Note that var myJar = Jar('abc') doesn't work.
Well, it does work, it just doesn't do anything expected/useful. In
that case the - this - value for the execution of the function is a
reference to the global object (so a "member" property is added to the
global object) and as the function has no return statement the result
of the call expression is the undefined value.
3) This question actually refers to a particular writer's
JS If you have time please look at this.
function Container(param) {
function dec() {
if (secret > 0) {
secret -= 1;
return true;
} else {
return false;
}
}
this.member = param;
var secret = 3;
var that = this;
}
According to the author,
"The private method dec examines the secret instance
variable. If it is greater than zero, it decrements
secret and returns true. Otherwise it returns false.
It can be used to make this object limited to three
uses."
My question is: How do you access dec()?
In that code you cannot access it. Indeed, the function object
resulting form the function declaration for - dec - will be garbage
collected (probably soon) after the constructor finishes execution.
I tried using
var myContainer = new Container('abc');
myContainer.dec() doesn't work!
No, it wouldn't. Given only the code above the object that results
form the - new - expression will have no "dec" property, and so
attempting to call - myContainer.dec - will error.